4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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.
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/>.
21 * Copyright (c) 2014, 2016, Intel Corporation.
24 * Amir Shehata <amir.shehata@intel.com>
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
44 #include <sys/ioctl.h>
46 #include <libcfs/util/ioctl.h>
47 #include <linux/lnet/lnetctl.h>
49 #include <sys/types.h>
53 #include "liblnetconfig.h"
56 #define CONFIG_CMD "configure"
57 #define UNCONFIG_CMD "unconfigure"
60 #define SHOW_CMD "show"
62 #define MANAGE_CMD "manage"
64 #define modparam_path "/sys/module/lnet/parameters/"
66 const char *gmsg_stat_names[] = {"sent_stats", "received_stats",
70 * lustre_lnet_ip_range_descr
71 * Describes an IP range.
72 * Each octect is an expression
74 struct lustre_lnet_ip_range_descr {
75 struct list_head ipr_entry;
76 struct list_head ipr_expr;
81 * Describes an ip2nets rule. This can be on a list of rules.
83 struct lustre_lnet_ip2nets {
84 struct lnet_dlc_network_descr ip2nets_net;
85 struct list_head ip2nets_ip_ranges;
88 int open_sysfs_file(const char *path, const char *attr, const int mode)
91 char filename[LNET_MAX_STR_LEN];
93 if (strlen(path) + strlen(attr) >= LNET_MAX_STR_LEN)
96 snprintf(filename, sizeof(filename), "%s%s",
99 fd = open(filename, mode);
104 static int read_sysfs_file(const char *path, const char *attr,
105 void *val, const size_t size, const int nelem)
108 int rc = LUSTRE_CFG_RC_GENERIC_ERR;
110 fd = open_sysfs_file(path, attr, O_RDONLY);
112 return LUSTRE_CFG_RC_NO_MATCH;
114 if (read(fd, val, size * nelem) == -1)
117 rc = LUSTRE_CFG_RC_NO_ERR;
124 static int write_sysfs_file(const char *path, const char *attr,
125 void *val, const size_t size, const int nelem)
128 int rc = LUSTRE_CFG_RC_GENERIC_ERR;
130 fd = open_sysfs_file(path, attr, O_WRONLY | O_TRUNC);
132 return LUSTRE_CFG_RC_NO_MATCH;
134 if (write(fd, val, size * nelem) == -1)
137 rc = LUSTRE_CFG_RC_NO_ERR;
146 * frees the memory allocated for an intf descriptor.
148 void free_intf_descr(struct lnet_dlc_intf_descr *intf_descr)
153 if (intf_descr->cpt_expr != NULL)
154 cfs_expr_list_free(intf_descr->cpt_expr);
159 * lustre_lnet_add_ip_range
161 * given a string of the format:
162 * <expr.expr.expr.expr> parse each expr into
163 * a lustre_lnet_ip_range_descr structure and insert on the list.
165 * This function is called from
166 * YAML on each ip-range.
167 * As a result of lnetctl command
168 * When building a NID or P2P selection rules
170 int lustre_lnet_add_ip_range(struct list_head *list, char *str_ip_range)
172 struct lustre_lnet_ip_range_descr *ip_range;
175 ip_range = calloc(1, sizeof(*ip_range));
176 if (ip_range == NULL)
177 return LUSTRE_CFG_RC_OUT_OF_MEM;
179 INIT_LIST_HEAD(&ip_range->ipr_entry);
180 INIT_LIST_HEAD(&ip_range->ipr_expr);
182 rc = cfs_ip_addr_parse(str_ip_range, strlen(str_ip_range),
183 &ip_range->ipr_expr);
185 return LUSTRE_CFG_RC_BAD_PARAM;
187 list_add_tail(&ip_range->ipr_entry, list);
189 return LUSTRE_CFG_RC_NO_ERR;
192 int lustre_lnet_add_intf_descr(struct list_head *list, char *intf, int len)
194 char *open_sq_bracket = NULL, *close_sq_bracket = NULL,
196 struct lnet_dlc_intf_descr *intf_descr = NULL;
198 char intf_string[LNET_MAX_STR_LEN];
200 if (len >= LNET_MAX_STR_LEN)
201 return LUSTRE_CFG_RC_BAD_PARAM;
203 strncpy(intf_string, intf, len);
204 intf_string[len] = '\0';
206 intf_descr = calloc(1, sizeof(*intf_descr));
207 if (intf_descr == NULL)
208 return LUSTRE_CFG_RC_OUT_OF_MEM;
210 INIT_LIST_HEAD(&intf_descr->intf_on_network);
212 intf_name = intf_string;
213 open_sq_bracket = strchr(intf_string, '[');
214 if (open_sq_bracket != NULL) {
215 close_sq_bracket = strchr(intf_string, ']');
216 if (close_sq_bracket == NULL) {
218 return LUSTRE_CFG_RC_BAD_PARAM;
220 rc = cfs_expr_list_parse(open_sq_bracket,
221 strlen(open_sq_bracket), 0, UINT_MAX,
222 &intf_descr->cpt_expr);
225 return LUSTRE_CFG_RC_BAD_PARAM;
227 strncpy(intf_descr->intf_name, intf_name,
228 open_sq_bracket - intf_name);
229 intf_descr->intf_name[open_sq_bracket - intf_name] = '\0';
231 strcpy(intf_descr->intf_name, intf_name);
232 intf_descr->cpt_expr = NULL;
235 list_add_tail(&intf_descr->intf_on_network, list);
237 return LUSTRE_CFG_RC_NO_ERR;
240 void lustre_lnet_init_nw_descr(struct lnet_dlc_network_descr *nw_descr)
242 if (nw_descr != NULL) {
244 INIT_LIST_HEAD(&nw_descr->network_on_rule);
245 INIT_LIST_HEAD(&nw_descr->nw_intflist);
249 int lustre_lnet_parse_nids(char *nids, char **array, int size,
253 char *comma = nids, *cur, *entry;
255 int i, len, start = 0, finish = 0;
257 if (nids == NULL || strlen(nids) == 0)
260 /* count the number or new nids, by counting the number of commas */
262 comma = strchr(comma, ',');
272 * if the array is not NULL allocate a large enough array to house
273 * the old and new entries
275 new_array = calloc(sizeof(char*),
276 (size > 0) ? size + num_nids : num_nids);
281 /* parse our the new nids and add them to the tail of the array */
284 start = (size > 0) ? size: 0;
285 finish = (size > 0) ? size + num_nids : num_nids;
286 for (i = start; i < finish; i++) {
287 comma = strchr(comma, ',');
290 * the length of the string to be parsed out is
291 * from cur to end of string. So it's good enough
294 len = strlen(cur) + 1;
296 /* length of the string is comma - cur */
297 len = (comma - cur) + 1;
299 entry = calloc(1, len);
301 finish = i > 0 ? i - 1: 0;
304 strncpy(entry, cur, len - 1);
306 new_array[i] = entry;
313 /* add the old entries in the array and delete the old array*/
314 for (i = 0; i < size; i++)
315 new_array[i] = array[i];
320 *out_array = new_array;
325 for (i = start; i < finish; i++)
335 * <intf>[<expr>], <intf>[<expr>],..
337 int lustre_lnet_parse_interfaces(char *intf_str,
338 struct lnet_dlc_network_descr *nw_descr)
343 char *cur = intf_str, *next = NULL;
344 char *end = intf_str + strlen(intf_str);
346 struct lnet_dlc_intf_descr *intf_descr, *tmp;
348 if (nw_descr == NULL)
349 return LUSTRE_CFG_RC_BAD_PARAM;
352 open_square = strchr(cur, '[');
353 if (open_square != NULL) {
354 close_square = strchr(cur, ']');
355 if (close_square == NULL) {
356 rc = LUSTRE_CFG_RC_BAD_PARAM;
360 comma = strchr(cur, ',');
361 if (comma != NULL && comma > close_square) {
363 len = next - close_square;
369 comma = strchr(cur, ',');
379 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist, cur, len);
380 if (rc != LUSTRE_CFG_RC_NO_ERR)
386 return LUSTRE_CFG_RC_NO_ERR;
389 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
391 list_del(&intf_descr->intf_on_network);
392 free_intf_descr(intf_descr);
398 int lustre_lnet_config_lib_init(void)
400 return register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH,
401 LNET_DEV_MAJOR, LNET_DEV_MINOR);
404 void lustre_lnet_config_lib_uninit(void)
406 unregister_ioc_dev(LNET_DEV_ID);
409 int lustre_lnet_config_ni_system(bool up, bool load_ni_from_mod,
410 int seq_no, struct cYAML **err_rc)
412 struct libcfs_ioctl_data data;
415 char err_str[LNET_MAX_STR_LEN];
417 snprintf(err_str, sizeof(err_str), "\"Success\"");
419 LIBCFS_IOC_INIT(data);
421 /* Reverse logic is used here in order not to change
422 * the lctl utility */
423 data.ioc_flags = load_ni_from_mod ? 0 : 1;
425 opc = up ? IOC_LIBCFS_CONFIGURE : IOC_LIBCFS_UNCONFIGURE;
427 rc = l_ioctl(LNET_DEV_ID, opc, &data);
431 "\"LNet %s error: %s\"", (up) ? "configure" :
432 "unconfigure", strerror(errno));
436 cYAML_build_error(rc, seq_no, (up) ? CONFIG_CMD : UNCONFIG_CMD,
437 "lnet", err_str, err_rc);
442 static lnet_nid_t *allocate_create_nid_array(char **nids, __u32 num_nids,
445 lnet_nid_t *array = NULL;
448 if (!nids || num_nids == 0) {
449 snprintf(err_str, LNET_MAX_STR_LEN, "no NIDs to add");
453 array = calloc(sizeof(*array) * num_nids, 1);
455 snprintf(err_str, LNET_MAX_STR_LEN, "out of memory");
459 for (i = 0; i < num_nids; i++) {
460 array[i] = libcfs_str2nid(nids[i]);
461 if (array[i] == LNET_NID_ANY) {
463 snprintf(err_str, LNET_MAX_STR_LEN,
473 static int dispatch_peer_ni_cmd(lnet_nid_t pnid, lnet_nid_t nid, __u32 cmd,
474 struct lnet_ioctl_peer_cfg *data,
475 char *err_str, char *cmd_str)
479 data->prcfg_prim_nid = pnid;
480 data->prcfg_cfg_nid = nid;
482 rc = l_ioctl(LNET_DEV_ID, cmd, data);
487 "\"cannot %s peer ni: %s\"",
488 (cmd_str) ? cmd_str : "add", strerror(errno));
494 static int infra_ping_nid(char *ping_nids, char *oper, int param, int ioc_call,
495 int seq_no, struct cYAML **show_rc,
496 struct cYAML **err_rc)
499 struct lnet_ioctl_ping_data ping;
500 struct cYAML *root = NULL, *ping_node = NULL, *item = NULL,
501 *first_seq = NULL, *tmp = NULL, *peer_ni = NULL;
502 lnet_process_id_t id;
503 char err_str[LNET_MAX_STR_LEN] = {0};
504 char *sep, *token, *end;
507 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
511 len = (sizeof(lnet_process_id_t) * LNET_INTERFACES_MAX_DEFAULT);
513 data = calloc(1, len);
517 /* create struct cYAML root object */
518 root = cYAML_create_object(NULL, NULL);
522 ping_node = cYAML_create_seq(root, oper);
523 if (ping_node == NULL)
526 /* tokenise each nid in string ping_nids */
527 token = strtok(ping_nids, ",");
530 item = cYAML_create_seq_item(ping_node);
534 if (first_seq == NULL)
537 /* check if '-' is a part of NID, token */
538 sep = strchr(token, '-');
540 id.pid = LNET_PID_ANY;
541 /* if no net is specified, libcfs_str2nid() will assume tcp */
542 id.nid = libcfs_str2nid(token);
543 if (id.nid == LNET_NID_ANY) {
544 snprintf(err_str, sizeof(err_str),
545 "\"cannot parse NID '%s'\"",
547 rc = LUSTRE_CFG_RC_BAD_PARAM;
548 cYAML_build_error(rc, seq_no, MANAGE_CMD,
549 oper, err_str, err_rc);
553 if (token[0] == 'u' || token[0] == 'U')
554 id.pid = (strtoul(&token[1], &end, 0) |
555 (LNET_PID_USERFLAG));
557 id.pid = strtoul(token, &end, 0);
559 /* assuming '-' is part of hostname */
561 id.pid = LNET_PID_ANY;
562 id.nid = libcfs_str2nid(token);
563 if (id.nid == LNET_NID_ANY) {
564 snprintf(err_str, sizeof(err_str),
565 "\"cannot parse NID '%s'\"",
567 rc = LUSTRE_CFG_RC_BAD_PARAM;
568 cYAML_build_error(rc, seq_no, MANAGE_CMD,
574 id.nid = libcfs_str2nid(sep + 1);
575 if (id.nid == LNET_NID_ANY) {
576 snprintf(err_str, sizeof(err_str),
577 "\"cannot parse NID '%s'\"",
579 rc = LUSTRE_CFG_RC_BAD_PARAM;
580 cYAML_build_error(rc, seq_no, MANAGE_CMD,
587 LIBCFS_IOC_INIT_V2(ping, ping_hdr);
588 ping.ping_hdr.ioc_len = sizeof(ping);
590 ping.op_param = param;
591 ping.ping_count = LNET_INTERFACES_MAX_DEFAULT;
592 ping.ping_buf = data;
594 rc = l_ioctl(LNET_DEV_ID, ioc_call, &ping);
597 sizeof(err_str), "failed to %s %s: %s\n", oper,
598 id.pid == LNET_PID_ANY ?
599 libcfs_nid2str(id.nid) :
600 libcfs_id2str(id), strerror(errno));
601 rc = LUSTRE_CFG_RC_BAD_PARAM;
602 cYAML_build_error(rc, seq_no, MANAGE_CMD,
603 oper, err_str, err_rc);
607 if (cYAML_create_string(item, "primary nid",
608 libcfs_nid2str(ping.ping_id.nid)) == NULL)
611 if (cYAML_create_string(item, "Multi-Rail", ping.mr_info ?
612 "True" : "False") == NULL)
615 tmp = cYAML_create_seq(item, "peer ni");
619 for (i = 0; i < ping.ping_count; i++) {
620 if (!strcmp(libcfs_nid2str(ping.ping_buf[i].nid),
623 peer_ni = cYAML_create_seq_item(tmp);
626 memset(buf, 0, sizeof buf);
627 snprintf(buf, sizeof buf, "nid");
628 if (cYAML_create_string(peer_ni, buf,
629 libcfs_nid2str(ping.ping_buf[i].nid)) == NULL)
635 } while ((token = strtok(NULL, ",")) != NULL);
638 rc = LUSTRE_CFG_RC_NO_ERR;
643 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
644 cYAML_free_tree(root);
645 } else if (show_rc != NULL && *show_rc != NULL) {
646 struct cYAML *show_node;
647 show_node = cYAML_get_object_item(*show_rc, oper);
648 if (show_node != NULL && cYAML_is_sequence(show_node)) {
649 cYAML_insert_child(show_node, first_seq);
652 } else if (show_node == NULL) {
653 cYAML_insert_sibling((*show_rc)->cy_child,
657 cYAML_free_tree(root);
666 int lustre_lnet_ping_nid(char *ping_nids, int timeout, int seq_no,
667 struct cYAML **show_rc, struct cYAML **err_rc)
671 rc = infra_ping_nid(ping_nids, "ping", timeout, IOC_LIBCFS_PING_PEER,
672 seq_no, show_rc, err_rc);
676 int lustre_lnet_discover_nid(char *ping_nids, int force, int seq_no,
677 struct cYAML **show_rc, struct cYAML **err_rc)
681 rc = infra_ping_nid(ping_nids, "discover", force, IOC_LIBCFS_DISCOVER,
682 seq_no, show_rc, err_rc);
686 int lustre_lnet_config_peer_nid(char *pnid, char **nid, int num_nids,
687 bool mr, int seq_no, struct cYAML **err_rc)
689 struct lnet_ioctl_peer_cfg data;
690 lnet_nid_t prim_nid = LNET_NID_ANY;
691 int rc = LUSTRE_CFG_RC_NO_ERR;
693 bool nid0_used = false;
694 char err_str[LNET_MAX_STR_LEN] = {0};
695 lnet_nid_t *nids = allocate_create_nid_array(nid, num_nids, err_str);
698 prim_nid = libcfs_str2nid(pnid);
699 if (prim_nid == LNET_NID_ANY) {
700 snprintf(err_str, sizeof(err_str),
703 rc = LUSTRE_CFG_RC_MISSING_PARAM;
706 } else if (!nids || nids[0] == LNET_NID_ANY) {
707 snprintf(err_str, sizeof(err_str),
708 "no NIDs provided for configuration");
709 rc = LUSTRE_CFG_RC_MISSING_PARAM;
712 prim_nid = LNET_NID_ANY;
715 snprintf(err_str, sizeof(err_str), "\"Success\"");
717 LIBCFS_IOC_INIT_V2(data, prcfg_hdr);
721 * if prim_nid is not specified use the first nid in the list of
722 * nids provided as the prim_nid. NOTE: on entering 'if' we must
723 * have at least 1 NID
725 if (prim_nid == LNET_NID_ANY) {
730 /* Create the prim_nid first */
731 rc = dispatch_peer_ni_cmd(prim_nid, LNET_NID_ANY,
732 IOC_LIBCFS_ADD_PEER_NI,
733 &data, err_str, "add");
738 /* add the rest of the nids to the key nid if any are available */
739 for (idx = nid0_used ? 1 : 0 ; nids && idx < num_nids; idx++) {
741 * If prim_nid is not provided then the first nid in the
742 * list becomes the prim_nid. First time round the loop use
743 * LNET_NID_ANY for the first parameter, then use nid[0]
744 * as the key nid after wards
746 rc = dispatch_peer_ni_cmd(prim_nid, nids[idx],
747 IOC_LIBCFS_ADD_PEER_NI, &data,
757 cYAML_build_error(rc, seq_no, ADD_CMD, "peer_ni", err_str, err_rc);
761 int lustre_lnet_del_peer_nid(char *pnid, char **nid, int num_nids,
762 int seq_no, struct cYAML **err_rc)
764 struct lnet_ioctl_peer_cfg data;
766 int rc = LUSTRE_CFG_RC_NO_ERR;
768 char err_str[LNET_MAX_STR_LEN] = {0};
769 lnet_nid_t *nids = allocate_create_nid_array(nid, num_nids, err_str);
772 snprintf(err_str, sizeof(err_str),
773 "\"Primary nid is not provided\"");
774 rc = LUSTRE_CFG_RC_MISSING_PARAM;
777 prim_nid = libcfs_str2nid(pnid);
778 if (prim_nid == LNET_NID_ANY) {
779 rc = LUSTRE_CFG_RC_BAD_PARAM;
780 snprintf(err_str, sizeof(err_str),
787 snprintf(err_str, sizeof(err_str), "\"Success\"");
789 LIBCFS_IOC_INIT_V2(data, prcfg_hdr);
790 if (!nids || nids[0] == LNET_NID_ANY) {
791 rc = dispatch_peer_ni_cmd(prim_nid, LNET_NID_ANY,
792 IOC_LIBCFS_DEL_PEER_NI,
793 &data, err_str, "del");
797 for (idx = 0; nids && idx < num_nids; idx++) {
798 rc = dispatch_peer_ni_cmd(prim_nid, nids[idx],
799 IOC_LIBCFS_DEL_PEER_NI, &data,
809 cYAML_build_error(rc, seq_no, DEL_CMD, "peer_ni", err_str, err_rc);
813 int lustre_lnet_config_route(char *nw, char *gw, int hops, int prio,
814 int seq_no, struct cYAML **err_rc)
816 struct lnet_ioctl_config_data data;
817 lnet_nid_t gateway_nid;
818 int rc = LUSTRE_CFG_RC_NO_ERR;
819 __u32 net = LNET_NIDNET(LNET_NID_ANY);
820 char err_str[LNET_MAX_STR_LEN];
822 snprintf(err_str, sizeof(err_str), "\"Success\"");
824 if (nw == NULL || gw == NULL) {
827 "\"missing mandatory parameter(s): '%s'\"",
828 (nw == NULL && gw == NULL) ? "network, gateway" :
829 (nw == NULL) ? "network" : "gateway");
830 rc = LUSTRE_CFG_RC_MISSING_PARAM;
834 net = libcfs_str2net(nw);
835 if (net == LNET_NIDNET(LNET_NID_ANY)) {
838 "\"cannot parse net %s\"", nw);
839 rc = LUSTRE_CFG_RC_BAD_PARAM;
843 gateway_nid = libcfs_str2nid(gw);
844 if (gateway_nid == LNET_NID_ANY) {
847 "\"cannot parse gateway NID '%s'\"", gw);
848 rc = LUSTRE_CFG_RC_BAD_PARAM;
853 /* hops is undefined */
854 hops = LNET_UNDEFINED_HOPS;
855 } else if (hops < 1 || hops > 255) {
858 "\"invalid hop count %d, must be between 1 and 255\"",
860 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
866 } else if (prio < 0) {
869 "\"invalid priority %d, must be greater than 0\"",
871 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
875 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
877 data.cfg_config_u.cfg_route.rtr_hop = hops;
878 data.cfg_config_u.cfg_route.rtr_priority = prio;
879 data.cfg_nid = gateway_nid;
881 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
886 "\"cannot add route: %s\"", strerror(errno));
891 cYAML_build_error(rc, seq_no, ADD_CMD, "route", err_str, err_rc);
896 int lustre_lnet_del_route(char *nw, char *gw,
897 int seq_no, struct cYAML **err_rc)
899 struct lnet_ioctl_config_data data;
900 lnet_nid_t gateway_nid;
901 int rc = LUSTRE_CFG_RC_NO_ERR;
902 __u32 net = LNET_NIDNET(LNET_NID_ANY);
903 char err_str[LNET_MAX_STR_LEN];
905 snprintf(err_str, sizeof(err_str), "\"Success\"");
907 if (nw == NULL || gw == NULL) {
910 "\"missing mandatory parameter(s): '%s'\"",
911 (nw == NULL && gw == NULL) ? "network, gateway" :
912 (nw == NULL) ? "network" : "gateway");
913 rc = LUSTRE_CFG_RC_MISSING_PARAM;
917 net = libcfs_str2net(nw);
918 if (net == LNET_NIDNET(LNET_NID_ANY)) {
921 "\"cannot parse net '%s'\"", nw);
922 rc = LUSTRE_CFG_RC_BAD_PARAM;
926 gateway_nid = libcfs_str2nid(gw);
927 if (gateway_nid == LNET_NID_ANY) {
930 "\"cannot parse gateway NID '%s'\"", gw);
931 rc = LUSTRE_CFG_RC_BAD_PARAM;
935 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
937 data.cfg_nid = gateway_nid;
939 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
944 "\"cannot delete route: %s\"", strerror(errno));
949 cYAML_build_error(rc, seq_no, DEL_CMD, "route", err_str, err_rc);
954 int lustre_lnet_show_route(char *nw, char *gw, int hops, int prio, int detail,
955 int seq_no, struct cYAML **show_rc,
956 struct cYAML **err_rc)
958 struct lnet_ioctl_config_data data;
959 lnet_nid_t gateway_nid;
960 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
962 __u32 net = LNET_NIDNET(LNET_NID_ANY);
964 struct cYAML *root = NULL, *route = NULL, *item = NULL;
965 struct cYAML *first_seq = NULL;
966 char err_str[LNET_MAX_STR_LEN];
969 snprintf(err_str, sizeof(err_str),
970 "\"out of memory\"");
973 net = libcfs_str2net(nw);
974 if (net == LNET_NIDNET(LNET_NID_ANY)) {
977 "\"cannot parse net '%s'\"", nw);
978 rc = LUSTRE_CFG_RC_BAD_PARAM;
983 /* show all routes without filtering on net */
984 net = LNET_NIDNET(LNET_NID_ANY);
988 gateway_nid = libcfs_str2nid(gw);
989 if (gateway_nid == LNET_NID_ANY) {
992 "\"cannot parse gateway NID '%s'\"", gw);
993 rc = LUSTRE_CFG_RC_BAD_PARAM;
997 /* show all routes with out filtering on gateway */
998 gateway_nid = LNET_NID_ANY;
1000 if ((hops < 1 && hops != -1) || hops > 255) {
1003 "\"invalid hop count %d, must be between 0 and 256\"",
1005 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1009 /* create struct cYAML root object */
1010 root = cYAML_create_object(NULL, NULL);
1014 route = cYAML_create_seq(root, "route");
1019 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1022 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
1028 /* filter on provided data */
1029 if (net != LNET_NIDNET(LNET_NID_ANY) &&
1030 net != data.cfg_net)
1033 if (gateway_nid != LNET_NID_ANY &&
1034 gateway_nid != data.cfg_nid)
1038 hops != data.cfg_config_u.cfg_route.rtr_hop)
1042 prio != data.cfg_config_u.cfg_route.rtr_priority)
1045 /* default rc to -1 incase we hit the goto */
1049 item = cYAML_create_seq_item(route);
1053 if (first_seq == NULL)
1056 if (cYAML_create_string(item, "net",
1057 libcfs_net2str(data.cfg_net)) == NULL)
1060 if (cYAML_create_string(item, "gateway",
1061 libcfs_nid2str(data.cfg_nid)) == NULL)
1065 if (cYAML_create_number(item, "hop",
1066 (int) data.cfg_config_u.
1067 cfg_route.rtr_hop) ==
1071 if (cYAML_create_number(item, "priority",
1073 cfg_route.rtr_priority) == NULL)
1076 if (cYAML_create_string(item, "state",
1077 data.cfg_config_u.cfg_route.
1079 "up" : "down") == NULL)
1084 /* print output iff show_rc is not provided */
1085 if (show_rc == NULL)
1086 cYAML_print_tree(root);
1088 if (l_errno != ENOENT) {
1091 "\"cannot get routes: %s\"",
1096 rc = LUSTRE_CFG_RC_NO_ERR;
1098 snprintf(err_str, sizeof(err_str), "\"success\"");
1100 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
1101 cYAML_free_tree(root);
1102 } else if (show_rc != NULL && *show_rc != NULL) {
1103 struct cYAML *show_node;
1104 /* find the route node, if one doesn't exist then
1105 * insert one. Otherwise add to the one there
1107 show_node = cYAML_get_object_item(*show_rc, "route");
1108 if (show_node != NULL && cYAML_is_sequence(show_node)) {
1109 cYAML_insert_child(show_node, first_seq);
1112 } else if (show_node == NULL) {
1113 cYAML_insert_sibling((*show_rc)->cy_child,
1117 cYAML_free_tree(root);
1123 cYAML_build_error(rc, seq_no, SHOW_CMD, "route", err_str, err_rc);
1128 static int socket_intf_query(int request, char *intf,
1134 if (strlen(intf) >= IFNAMSIZ || ifr == NULL)
1135 return LUSTRE_CFG_RC_BAD_PARAM;
1137 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
1139 return LUSTRE_CFG_RC_BAD_PARAM;
1141 strcpy(ifr->ifr_name, intf);
1142 rc = ioctl(sockfd, request, ifr);
1144 rc = LUSTRE_CFG_RC_BAD_PARAM;
1152 * for each interface in the array of interfaces find the IP address of
1153 * that interface, create its nid and add it to an array of NIDs.
1154 * Stop if any of the interfaces is down
1156 static int lustre_lnet_intf2nids(struct lnet_dlc_network_descr *nw,
1157 lnet_nid_t **nids, __u32 *nnids)
1159 int i = 0, count = 0, rc;
1162 struct lnet_dlc_intf_descr *intf;
1164 if (nw == NULL || nids == NULL)
1165 return LUSTRE_CFG_RC_BAD_PARAM;
1167 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network)
1170 *nids = calloc(count, sizeof(lnet_nid_t));
1172 return LUSTRE_CFG_RC_OUT_OF_MEM;
1174 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network) {
1175 memset(&ifr, 0, sizeof(ifr));
1176 rc = socket_intf_query(SIOCGIFFLAGS, intf->intf_name, &ifr);
1180 if ((ifr.ifr_flags & IFF_UP) == 0) {
1181 rc = LUSTRE_CFG_RC_BAD_PARAM;
1185 memset(&ifr, 0, sizeof(ifr));
1186 rc = socket_intf_query(SIOCGIFADDR, intf->intf_name, &ifr);
1190 ip = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
1192 (*nids)[i] = LNET_MKNID(nw->nw_id, ip);
1207 * called repeatedly until a match or no more ip range
1209 * ip_range expression
1210 * interface list with all the interface names.
1211 * all the interfaces in the system.
1213 * try to match the ip_range expr to one of the interfaces' IPs in
1214 * the system. If we hit a patch for an interface. Check if that
1215 * interface name is in the list.
1217 * If there are more than one interface in the list, then make sure
1218 * that the IPs for all of these interfaces match the ip ranges
1221 * for each interface in intf_list
1222 * look up the intf name in ifa
1223 * if not there then no match
1224 * check ip obtained from ifa against a match to any of the
1226 * If no match, then fail
1228 * The result is that all the interfaces have to match.
1230 int lustre_lnet_match_ip_to_intf(struct ifaddrs *ifa,
1231 struct list_head *intf_list,
1232 struct list_head *ip_ranges)
1236 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1237 struct ifaddrs *ifaddr = ifa;
1238 struct lustre_lnet_ip_range_descr *ip_range;
1242 * if there are no explicit interfaces, and no ip ranges, then
1243 * configure the first tcp interface we encounter.
1245 if (list_empty(intf_list) && list_empty(ip_ranges)) {
1246 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1247 if (ifaddr->ifa_addr == NULL)
1250 if ((ifaddr->ifa_flags & IFF_UP) == 0)
1253 family = ifaddr->ifa_addr->sa_family;
1254 if (family == AF_INET &&
1255 strcmp(ifaddr->ifa_name, "lo") != 0) {
1256 rc = lustre_lnet_add_intf_descr
1257 (intf_list, ifaddr->ifa_name,
1258 strlen(ifaddr->ifa_name));
1260 if (rc != LUSTRE_CFG_RC_NO_ERR)
1263 return LUSTRE_CFG_RC_MATCH;
1266 return LUSTRE_CFG_RC_NO_MATCH;
1270 * First interface which matches an IP pattern will be used
1272 if (list_empty(intf_list)) {
1274 * no interfaces provided in the rule, but an ip range is
1275 * provided, so try and match an interface to the ip
1278 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1279 if (ifaddr->ifa_addr == NULL)
1282 if ((ifaddr->ifa_flags & IFF_UP) == 0)
1285 family = ifaddr->ifa_addr->sa_family;
1286 if (family == AF_INET) {
1287 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->
1290 list_for_each_entry(ip_range, ip_ranges,
1292 rc = cfs_ip_addr_match(bswap_32(ip),
1293 &ip_range->ipr_expr);
1297 rc = lustre_lnet_add_intf_descr
1298 (intf_list, ifaddr->ifa_name,
1299 strlen(ifaddr->ifa_name));
1301 if (rc != LUSTRE_CFG_RC_NO_ERR)
1307 if (!list_empty(intf_list))
1308 return LUSTRE_CFG_RC_MATCH;
1310 return LUSTRE_CFG_RC_NO_MATCH;
1314 * If an interface is explicitly specified the ip-range might or
1315 * might not be specified. if specified the interface needs to match the
1316 * ip-range. If no ip-range then the interfaces are
1317 * automatically matched if they are all up.
1318 * If > 1 interfaces all the interfaces must match for the NI to
1321 list_for_each_entry_safe(intf_descr, tmp, intf_list, intf_on_network) {
1322 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1323 if (ifaddr->ifa_addr == NULL)
1326 family = ifaddr->ifa_addr->sa_family;
1327 if (family == AF_INET &&
1328 strcmp(intf_descr->intf_name,
1329 ifaddr->ifa_name) == 0)
1333 if (ifaddr == NULL) {
1334 list_del(&intf_descr->intf_on_network);
1335 free_intf_descr(intf_descr);
1339 if ((ifaddr->ifa_flags & IFF_UP) == 0) {
1340 list_del(&intf_descr->intf_on_network);
1341 free_intf_descr(intf_descr);
1345 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr.s_addr;
1348 list_for_each_entry(ip_range, ip_ranges, ipr_entry) {
1349 rc = cfs_ip_addr_match(bswap_32(ip), &ip_range->ipr_expr);
1355 /* no match for this interface */
1356 list_del(&intf_descr->intf_on_network);
1357 free_intf_descr(intf_descr);
1361 return LUSTRE_CFG_RC_MATCH;
1364 int lustre_lnet_resolve_ip2nets_rule(struct lustre_lnet_ip2nets *ip2nets,
1365 lnet_nid_t **nids, __u32 *nnids)
1367 struct ifaddrs *ifa;
1368 int rc = LUSTRE_CFG_RC_NO_ERR;
1370 rc = getifaddrs(&ifa);
1374 rc = lustre_lnet_match_ip_to_intf(ifa,
1375 &ip2nets->ip2nets_net.nw_intflist,
1376 &ip2nets->ip2nets_ip_ranges);
1377 if (rc != LUSTRE_CFG_RC_MATCH) {
1382 rc = lustre_lnet_intf2nids(&ip2nets->ip2nets_net, nids, nnids);
1383 if (rc != LUSTRE_CFG_RC_NO_ERR) {
1394 lustre_lnet_ioctl_config_ni(struct list_head *intf_list,
1395 struct lnet_ioctl_config_lnd_tunables *tunables,
1396 struct cfs_expr_list *global_cpts,
1397 lnet_nid_t *nids, char *err_str)
1400 struct lnet_ioctl_config_ni *conf;
1401 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1402 int rc = LUSTRE_CFG_RC_NO_ERR, i = 0;
1405 struct lnet_dlc_intf_descr *intf_descr;
1407 struct cfs_expr_list *cpt_expr;
1409 list_for_each_entry(intf_descr, intf_list,
1411 if (tunables != NULL)
1412 len = sizeof(struct lnet_ioctl_config_ni) +
1413 sizeof(struct lnet_ioctl_config_lnd_tunables);
1415 len = sizeof(struct lnet_ioctl_config_ni);
1417 data = calloc(1, len);
1419 return LUSTRE_CFG_RC_OUT_OF_MEM;
1420 conf = (struct lnet_ioctl_config_ni*) data;
1421 if (tunables != NULL)
1422 tun = (struct lnet_ioctl_config_lnd_tunables*)
1425 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1426 conf->lic_cfg_hdr.ioc_len = len;
1427 conf->lic_nid = nids[i];
1428 strncpy(conf->lic_ni_intf[0], intf_descr->intf_name,
1431 if (intf_descr->cpt_expr != NULL)
1432 cpt_expr = intf_descr->cpt_expr;
1433 else if (global_cpts != NULL)
1434 cpt_expr = global_cpts;
1438 if (cpt_expr != NULL) {
1439 count = cfs_expr_list_values(cpt_expr,
1440 LNET_MAX_SHOW_NUM_CPT,
1443 memcpy(conf->lic_cpts, cpt_array,
1444 sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1453 conf->lic_ncpts = count;
1455 if (tunables != NULL)
1456 memcpy(tun, tunables, sizeof(*tunables));
1458 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1463 "\"cannot add network: %s\"", strerror(errno));
1471 return LUSTRE_CFG_RC_NO_ERR;
1475 lustre_lnet_config_ip2nets(struct lustre_lnet_ip2nets *ip2nets,
1476 struct lnet_ioctl_config_lnd_tunables *tunables,
1477 struct cfs_expr_list *global_cpts,
1478 int seq_no, struct cYAML **err_rc)
1480 lnet_nid_t *nids = NULL;
1483 char err_str[LNET_MAX_STR_LEN];
1485 snprintf(err_str, sizeof(err_str), "\"success\"");
1490 "\"incomplete ip2nets information\"");
1491 rc = LUSTRE_CFG_RC_BAD_PARAM;
1496 * call below function to resolve the rules into a list of nids.
1497 * The memory is allocated in that function then freed here when
1498 * it's no longer needed.
1500 rc = lustre_lnet_resolve_ip2nets_rule(ip2nets, &nids, &nnids);
1501 if (rc != LUSTRE_CFG_RC_NO_ERR && rc != LUSTRE_CFG_RC_MATCH) {
1504 "\"cannot resolve ip2nets rule\"");
1508 if (list_empty(&ip2nets->ip2nets_net.nw_intflist)) {
1509 snprintf(err_str, sizeof(err_str),
1510 "\"no interfaces match ip2nets rules\"");
1514 rc = lustre_lnet_ioctl_config_ni(&ip2nets->ip2nets_net.nw_intflist,
1515 tunables, global_cpts, nids,
1522 cYAML_build_error(rc, seq_no, ADD_CMD, "ip2nets", err_str, err_rc);
1526 int lustre_lnet_config_ni(struct lnet_dlc_network_descr *nw_descr,
1527 struct cfs_expr_list *global_cpts,
1529 struct lnet_ioctl_config_lnd_tunables *tunables,
1530 int seq_no, struct cYAML **err_rc)
1533 struct lnet_ioctl_config_ni *conf;
1534 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1535 char buf[LNET_MAX_STR_LEN];
1536 int rc = LUSTRE_CFG_RC_NO_ERR;
1537 char err_str[LNET_MAX_STR_LEN];
1538 lnet_nid_t *nids = NULL;
1542 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1545 snprintf(err_str, sizeof(err_str), "\"success\"");
1547 if (ip2net == NULL && (nw_descr == NULL || nw_descr->nw_id == 0 ||
1548 list_empty(&nw_descr->nw_intflist))) {
1551 "\"missing mandatory parameters\"");
1552 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1556 if (ip2net != NULL && strlen(ip2net) >= sizeof(buf)) {
1559 "\"ip2net string too long %d\"",
1560 (int)strlen(ip2net));
1561 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1565 if (ip2net != NULL) {
1566 if (tunables != NULL)
1567 len = sizeof(struct lnet_ioctl_config_ni) +
1568 sizeof(struct lnet_ioctl_config_lnd_tunables);
1570 len = sizeof(struct lnet_ioctl_config_ni);
1571 data = calloc(1, len);
1573 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1576 conf = (struct lnet_ioctl_config_ni*) data;
1577 if (tunables != NULL)
1578 tun = (struct lnet_ioctl_config_lnd_tunables*)
1579 (data + sizeof(*conf));
1581 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1582 conf->lic_cfg_hdr.ioc_len = len;
1583 strncpy(conf->lic_legacy_ip2nets, ip2net,
1586 if (global_cpts != NULL) {
1587 count = cfs_expr_list_values(global_cpts,
1588 LNET_MAX_SHOW_NUM_CPT,
1591 memcpy(conf->lic_cpts, cpt_array,
1592 sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1601 conf->lic_ncpts = count;
1603 if (tunables != NULL)
1604 memcpy(tun, tunables, sizeof(*tunables));
1606 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1611 "\"cannot add network: %s\"", strerror(errno));
1618 if (LNET_NETTYP(nw_descr->nw_id) == LOLND) {
1619 rc = LUSTRE_CFG_RC_NO_ERR;
1623 if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1626 "\"cannot parse net '%s'\"",
1627 libcfs_net2str(nw_descr->nw_id));
1628 rc = LUSTRE_CFG_RC_BAD_PARAM;
1632 if (list_empty(&nw_descr->nw_intflist)) {
1635 "\"no interface name provided\"");
1636 rc = LUSTRE_CFG_RC_BAD_PARAM;
1640 rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids);
1642 snprintf(err_str, sizeof(err_str),
1643 "\"bad parameter\"");
1644 rc = LUSTRE_CFG_RC_BAD_PARAM;
1648 rc = lustre_lnet_ioctl_config_ni(&nw_descr->nw_intflist,
1649 tunables, global_cpts, nids,
1653 if (nw_descr != NULL) {
1654 list_for_each_entry_safe(intf_descr, tmp,
1655 &nw_descr->nw_intflist,
1657 list_del(&intf_descr->intf_on_network);
1658 free_intf_descr(intf_descr);
1662 cYAML_build_error(rc, seq_no, ADD_CMD, "net", err_str, err_rc);
1673 int lustre_lnet_del_ni(struct lnet_dlc_network_descr *nw_descr,
1674 int seq_no, struct cYAML **err_rc)
1676 struct lnet_ioctl_config_ni data;
1677 int rc = LUSTRE_CFG_RC_NO_ERR, i;
1678 char err_str[LNET_MAX_STR_LEN];
1679 lnet_nid_t *nids = NULL;
1681 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1683 snprintf(err_str, sizeof(err_str), "\"success\"");
1685 if (nw_descr == NULL || nw_descr->nw_id == 0 ||
1686 list_empty(&nw_descr->nw_intflist)) {
1689 "\"missing mandatory parameter\"");
1690 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1694 if (LNET_NETTYP(nw_descr->nw_id) == LOLND)
1695 return LUSTRE_CFG_RC_NO_ERR;
1697 if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1700 "\"cannot parse net '%s'\"",
1701 libcfs_net2str(nw_descr->nw_id));
1702 rc = LUSTRE_CFG_RC_BAD_PARAM;
1706 rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids);
1708 snprintf(err_str, sizeof(err_str),
1709 "\"bad parameter\"");
1710 rc = LUSTRE_CFG_RC_BAD_PARAM;
1715 * no interfaces just the nw_id is specified
1718 nids = calloc(1, sizeof(*nids));
1720 snprintf(err_str, sizeof(err_str),
1721 "\"out of memory\"");
1722 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1725 nids[0] = LNET_MKNID(nw_descr->nw_id, 0);
1729 for (i = 0; i < nnids; i++) {
1730 LIBCFS_IOC_INIT_V2(data, lic_cfg_hdr);
1731 data.lic_nid = nids[i];
1733 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_LOCAL_NI, &data);
1738 "\"cannot del network: %s\"", strerror(errno));
1742 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
1744 list_del(&intf_descr->intf_on_network);
1745 free_intf_descr(intf_descr);
1749 cYAML_build_error(rc, seq_no, DEL_CMD, "net", err_str, err_rc);
1758 add_msg_stats_to_yaml_blk(struct cYAML *yaml,
1759 struct lnet_ioctl_comm_count *counts)
1761 if (cYAML_create_number(yaml, "put",
1762 counts->ico_put_count)
1765 if (cYAML_create_number(yaml, "get",
1766 counts->ico_get_count)
1769 if (cYAML_create_number(yaml, "reply",
1770 counts->ico_reply_count)
1773 if (cYAML_create_number(yaml, "ack",
1774 counts->ico_ack_count)
1777 if (cYAML_create_number(yaml, "hello",
1778 counts->ico_hello_count)
1785 static struct lnet_ioctl_comm_count *
1786 get_counts(struct lnet_ioctl_element_msg_stats *msg_stats, int idx)
1789 return &msg_stats->im_send_stats;
1791 return &msg_stats->im_recv_stats;
1793 return &msg_stats->im_drop_stats;
1798 int lustre_lnet_show_net(char *nw, int detail, int seq_no,
1799 struct cYAML **show_rc, struct cYAML **err_rc)
1802 struct lnet_ioctl_config_ni *ni_data;
1803 struct lnet_ioctl_config_lnd_tunables *lnd;
1804 struct lnet_ioctl_element_stats *stats;
1805 struct lnet_ioctl_element_msg_stats msg_stats;
1806 __u32 net = LNET_NIDNET(LNET_NID_ANY);
1807 __u32 prev_net = LNET_NIDNET(LNET_NID_ANY);
1808 int rc = LUSTRE_CFG_RC_OUT_OF_MEM, i, j;
1810 struct cYAML *root = NULL, *tunables = NULL,
1811 *net_node = NULL, *interfaces = NULL,
1812 *item = NULL, *first_seq = NULL,
1813 *tmp = NULL, *statistics = NULL;
1814 int str_buf_len = LNET_MAX_SHOW_NUM_CPT * 2;
1815 char str_buf[str_buf_len];
1817 char err_str[LNET_MAX_STR_LEN];
1818 bool exist = false, new_net = true;
1820 size_t buf_size = sizeof(*ni_data) + sizeof(*lnd) + sizeof(*stats);
1822 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1824 buf = calloc(1, buf_size);
1828 ni_data = (struct lnet_ioctl_config_ni *)buf;
1831 net = libcfs_str2net(nw);
1832 if (net == LNET_NIDNET(LNET_NID_ANY)) {
1835 "\"cannot parse net '%s'\"", nw);
1836 rc = LUSTRE_CFG_RC_BAD_PARAM;
1841 root = cYAML_create_object(NULL, NULL);
1845 net_node = cYAML_create_seq(root, "net");
1846 if (net_node == NULL)
1853 memset(buf, 0, buf_size);
1855 LIBCFS_IOC_INIT_V2(*ni_data, lic_cfg_hdr);
1857 * set the ioc_len to the proper value since INIT assumes
1860 ni_data->lic_cfg_hdr.ioc_len = buf_size;
1861 ni_data->lic_idx = i;
1863 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LOCAL_NI, ni_data);
1869 rc_net = LNET_NIDNET(ni_data->lic_nid);
1871 /* filter on provided data */
1872 if (net != LNET_NIDNET(LNET_NID_ANY) &&
1876 /* default rc to -1 in case we hit the goto */
1880 stats = (struct lnet_ioctl_element_stats *)ni_data->lic_bulk;
1881 lnd = (struct lnet_ioctl_config_lnd_tunables *)
1882 (ni_data->lic_bulk + sizeof(*stats));
1884 if (rc_net != prev_net) {
1891 if (!cYAML_create_string(net_node, "net type",
1892 libcfs_net2str(rc_net)))
1895 tmp = cYAML_create_seq(net_node, "local NI(s)");
1901 /* create the tree to be printed. */
1902 item = cYAML_create_seq_item(tmp);
1906 if (first_seq == NULL)
1909 if (cYAML_create_string(item, "nid",
1910 libcfs_nid2str(ni_data->lic_nid)) == NULL)
1913 if (cYAML_create_string(item,
1915 (ni_data->lic_status ==
1916 LNET_NI_STATUS_UP) ?
1917 "up" : "down") == NULL)
1920 /* don't add interfaces unless there is at least one
1922 if (strlen(ni_data->lic_ni_intf[0]) > 0) {
1923 interfaces = cYAML_create_object(item, "interfaces");
1924 if (interfaces == NULL)
1927 for (j = 0; j < LNET_INTERFACES_NUM; j++) {
1928 if (strlen(ni_data->lic_ni_intf[j]) > 0) {
1930 sizeof(str_buf), "%d", j);
1931 if (cYAML_create_string(interfaces,
1933 ni_data->lic_ni_intf[j]) ==
1944 statistics = cYAML_create_object(item, "statistics");
1945 if (statistics == NULL)
1948 if (cYAML_create_number(statistics, "send_count",
1949 stats->iel_send_count)
1953 if (cYAML_create_number(statistics, "recv_count",
1954 stats->iel_recv_count)
1958 if (cYAML_create_number(statistics, "drop_count",
1959 stats->iel_drop_count)
1964 goto continue_without_msg_stats;
1966 LIBCFS_IOC_INIT_V2(msg_stats, im_hdr);
1967 msg_stats.im_hdr.ioc_len = sizeof(msg_stats);
1968 msg_stats.im_idx = i;
1970 rc = l_ioctl(LNET_DEV_ID,
1971 IOC_LIBCFS_GET_LOCAL_NI_MSG_STATS,
1975 goto continue_without_msg_stats;
1978 for (k = 0; k < 3; k++) {
1979 struct lnet_ioctl_comm_count *counts;
1980 struct cYAML *msg_statistics = NULL;
1982 msg_statistics = cYAML_create_object(item,
1983 (char *)gmsg_stat_names[k]);
1984 if (msg_statistics == NULL)
1987 counts = get_counts(&msg_stats, k);
1991 if (!add_msg_stats_to_yaml_blk(msg_statistics,
1996 continue_without_msg_stats:
1997 tunables = cYAML_create_object(item, "tunables");
2001 rc = lustre_net_show_tunables(tunables, &lnd->lt_cmn);
2002 if (rc != LUSTRE_CFG_RC_NO_ERR)
2005 tunables = cYAML_create_object(item, "lnd tunables");
2006 if (tunables == NULL)
2009 rc = lustre_ni_show_tunables(tunables, LNET_NETTYP(rc_net),
2011 if (rc != LUSTRE_CFG_RC_NO_ERR)
2014 if (cYAML_create_number(item, "tcp bonding",
2015 ni_data->lic_tcp_bonding)
2019 if (cYAML_create_number(item, "dev cpt",
2020 ni_data->lic_dev_cpt) == NULL)
2023 /* out put the CPTs in the format: "[x,x,x,...]" */
2024 limit = str_buf + str_buf_len - 3;
2025 pos += snprintf(pos, limit - pos, "\"[");
2026 for (j = 0 ; ni_data->lic_ncpts >= 1 &&
2027 j < ni_data->lic_ncpts &&
2029 pos += snprintf(pos, limit - pos,
2030 "%d", ni_data->lic_cpts[j]);
2031 if ((j + 1) < ni_data->lic_ncpts)
2032 pos += snprintf(pos, limit - pos, ",");
2034 pos += snprintf(pos, 3, "]\"");
2036 if (ni_data->lic_ncpts >= 1 &&
2037 cYAML_create_string(item, "CPT",
2043 /* Print out the net information only if show_rc is not provided */
2044 if (show_rc == NULL)
2045 cYAML_print_tree(root);
2047 if (l_errno != ENOENT) {
2050 "\"cannot get networks: %s\"",
2055 rc = LUSTRE_CFG_RC_NO_ERR;
2057 snprintf(err_str, sizeof(err_str), "\"success\"");
2059 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2060 cYAML_free_tree(root);
2061 } else if (show_rc != NULL && *show_rc != NULL) {
2062 struct cYAML *show_node;
2063 /* find the net node, if one doesn't exist
2064 * then insert one. Otherwise add to the one there
2066 show_node = cYAML_get_object_item(*show_rc, "net");
2067 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2068 cYAML_insert_child(show_node, first_seq);
2071 } else if (show_node == NULL) {
2072 cYAML_insert_sibling((*show_rc)->cy_child,
2076 cYAML_free_tree(root);
2082 cYAML_build_error(rc, seq_no, SHOW_CMD, "net", err_str, err_rc);
2087 int lustre_lnet_enable_routing(int enable, int seq_no, struct cYAML **err_rc)
2089 struct lnet_ioctl_config_data data;
2090 int rc = LUSTRE_CFG_RC_NO_ERR;
2091 char err_str[LNET_MAX_STR_LEN];
2093 snprintf(err_str, sizeof(err_str), "\"success\"");
2095 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2096 data.cfg_config_u.cfg_buffers.buf_enable = (enable) ? 1 : 0;
2098 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIG_RTR, &data);
2103 "\"cannot %s routing %s\"",
2104 (enable) ? "enable" : "disable", strerror(errno));
2109 cYAML_build_error(rc, seq_no,
2110 (enable) ? ADD_CMD : DEL_CMD,
2111 "routing", err_str, err_rc);
2116 int ioctl_set_value(__u32 val, int ioc, char *name,
2117 int seq_no, struct cYAML **err_rc)
2119 struct lnet_ioctl_set_value data;
2120 int rc = LUSTRE_CFG_RC_NO_ERR;
2121 char err_str[LNET_MAX_STR_LEN];
2123 snprintf(err_str, sizeof(err_str), "\"success\"");
2125 LIBCFS_IOC_INIT_V2(data, sv_hdr);
2126 data.sv_value = val;
2128 rc = l_ioctl(LNET_DEV_ID, ioc , &data);
2133 "\"cannot configure %s to %d: %s\"", name,
2134 val, strerror(errno));
2137 cYAML_build_error(rc, seq_no, ADD_CMD, name, err_str, err_rc);
2142 int lustre_lnet_config_max_intf(int max, int seq_no, struct cYAML **err_rc)
2144 int rc = LUSTRE_CFG_RC_NO_ERR;
2145 char err_str[LNET_MAX_STR_LEN];
2146 char val[LNET_MAX_STR_LEN];
2148 snprintf(err_str, sizeof(err_str), "\"success\"");
2150 snprintf(val, sizeof(val), "%d", max);
2152 rc = write_sysfs_file(modparam_path, "lnet_interfaces_max", val,
2153 1, strlen(val) + 1);
2155 snprintf(err_str, sizeof(err_str),
2156 "\"cannot configure max interfaces: %s\"",
2159 cYAML_build_error(rc, seq_no, ADD_CMD, "max_interfaces", err_str, err_rc);
2164 int lustre_lnet_config_discovery(int enable, int seq_no, struct cYAML **err_rc)
2166 int rc = LUSTRE_CFG_RC_NO_ERR;
2167 char err_str[LNET_MAX_STR_LEN];
2168 char val[LNET_MAX_STR_LEN];
2170 snprintf(err_str, sizeof(err_str), "\"success\"");
2172 snprintf(val, sizeof(val), "%u", (enable) ? 0 : 1);
2174 rc = write_sysfs_file(modparam_path, "lnet_peer_discovery_disabled", val,
2175 1, strlen(val) + 1);
2177 snprintf(err_str, sizeof(err_str),
2178 "\"cannot configure discovery: %s\"",
2181 cYAML_build_error(rc, seq_no, ADD_CMD, "discovery", err_str, err_rc);
2187 int lustre_lnet_config_numa_range(int range, int seq_no, struct cYAML **err_rc)
2189 return ioctl_set_value(range, IOC_LIBCFS_SET_NUMA_RANGE,
2190 "numa_range", seq_no, err_rc);
2193 int lustre_lnet_config_buffers(int tiny, int small, int large, int seq_no,
2194 struct cYAML **err_rc)
2196 struct lnet_ioctl_config_data data;
2197 int rc = LUSTRE_CFG_RC_NO_ERR;
2198 char err_str[LNET_MAX_STR_LEN];
2200 snprintf(err_str, sizeof(err_str), "\"success\"");
2202 /* -1 indicates to ignore changes to this field */
2203 if (tiny < -1 || small < -1 || large < -1) {
2206 "\"tiny, small and large must be >= 0\"");
2207 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
2211 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2212 data.cfg_config_u.cfg_buffers.buf_tiny = tiny;
2213 data.cfg_config_u.cfg_buffers.buf_small = small;
2214 data.cfg_config_u.cfg_buffers.buf_large = large;
2216 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_BUF, &data);
2221 "\"cannot configure buffers: %s\"", strerror(errno));
2226 cYAML_build_error(rc, seq_no, ADD_CMD, "buf", err_str, err_rc);
2231 int lustre_lnet_show_routing(int seq_no, struct cYAML **show_rc,
2232 struct cYAML **err_rc)
2234 struct lnet_ioctl_config_data *data;
2235 struct lnet_ioctl_pool_cfg *pool_cfg = NULL;
2236 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2239 char *pools[LNET_NRBPOOLS] = {"tiny", "small", "large"};
2240 int buf_count[LNET_NRBPOOLS] = {0};
2241 struct cYAML *root = NULL, *pools_node = NULL,
2242 *type_node = NULL, *item = NULL, *cpt = NULL,
2243 *first_seq = NULL, *buffers = NULL;
2245 char err_str[LNET_MAX_STR_LEN];
2246 char node_name[LNET_MAX_STR_LEN];
2249 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2251 buf = calloc(1, sizeof(*data) + sizeof(*pool_cfg));
2255 data = (struct lnet_ioctl_config_data *)buf;
2257 root = cYAML_create_object(NULL, NULL);
2261 pools_node = cYAML_create_seq(root, "routing");
2262 if (pools_node == NULL)
2266 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
2267 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
2268 sizeof(struct lnet_ioctl_pool_cfg);
2269 data->cfg_count = i;
2271 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_BUF, data);
2279 pool_cfg = (struct lnet_ioctl_pool_cfg *)data->cfg_bulk;
2281 snprintf(node_name, sizeof(node_name), "cpt[%d]", i);
2282 item = cYAML_create_seq_item(pools_node);
2286 if (first_seq == NULL)
2289 cpt = cYAML_create_object(item, node_name);
2293 /* create the tree and print */
2294 for (j = 0; j < LNET_NRBPOOLS; j++) {
2295 type_node = cYAML_create_object(cpt, pools[j]);
2296 if (type_node == NULL)
2298 if (cYAML_create_number(type_node, "npages",
2299 pool_cfg->pl_pools[j].pl_npages)
2302 if (cYAML_create_number(type_node, "nbuffers",
2303 pool_cfg->pl_pools[j].
2304 pl_nbuffers) == NULL)
2306 if (cYAML_create_number(type_node, "credits",
2307 pool_cfg->pl_pools[j].
2308 pl_credits) == NULL)
2310 if (cYAML_create_number(type_node, "mincredits",
2311 pool_cfg->pl_pools[j].
2312 pl_mincredits) == NULL)
2314 /* keep track of the total count for each of the
2315 * tiny, small and large buffers */
2316 buf_count[j] += pool_cfg->pl_pools[j].pl_nbuffers;
2320 if (pool_cfg != NULL) {
2321 item = cYAML_create_seq_item(pools_node);
2325 if (cYAML_create_number(item, "enable", pool_cfg->pl_routing) ==
2330 /* create a buffers entry in the show. This is necessary so that
2331 * if the YAML output is used to configure a node, the buffer
2332 * configuration takes hold */
2333 buffers = cYAML_create_object(root, "buffers");
2334 if (buffers == NULL)
2337 for (i = 0; i < LNET_NRBPOOLS; i++) {
2338 if (cYAML_create_number(buffers, pools[i], buf_count[i]) == NULL)
2342 if (show_rc == NULL)
2343 cYAML_print_tree(root);
2345 if (l_errno != ENOENT) {
2348 "\"cannot get routing information: %s\"",
2353 rc = LUSTRE_CFG_RC_NO_ERR;
2355 snprintf(err_str, sizeof(err_str), "\"success\"");
2356 rc = LUSTRE_CFG_RC_NO_ERR;
2360 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2361 cYAML_free_tree(root);
2362 } else if (show_rc != NULL && *show_rc != NULL) {
2363 struct cYAML *routing_node;
2364 /* there should exist only one routing block and one
2365 * buffers block. If there already exists a previous one
2366 * then don't add another */
2367 routing_node = cYAML_get_object_item(*show_rc, "routing");
2368 if (routing_node == NULL) {
2369 cYAML_insert_sibling((*show_rc)->cy_child,
2373 cYAML_free_tree(root);
2379 cYAML_build_error(rc, seq_no, SHOW_CMD, "routing", err_str, err_rc);
2384 int lustre_lnet_show_peer(char *knid, int detail, int seq_no,
2385 struct cYAML **show_rc, struct cYAML **err_rc)
2388 * TODO: This function is changing in a future patch to accommodate
2389 * PEER_LIST and proper filtering on any nid of the peer
2391 struct lnet_ioctl_peer_cfg peer_info;
2392 struct lnet_peer_ni_credit_info *lpni_cri;
2393 struct lnet_ioctl_element_stats *lpni_stats;
2394 struct lnet_ioctl_element_msg_stats *msg_stats;
2396 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2401 struct cYAML *root = NULL, *peer = NULL, *peer_ni = NULL,
2402 *first_seq = NULL, *peer_root = NULL, *tmp = NULL,
2403 *msg_statistics = NULL, *statistics = NULL;
2404 char err_str[LNET_MAX_STR_LEN];
2405 lnet_process_id_t *list = NULL;
2409 snprintf(err_str, sizeof(err_str),
2410 "\"out of memory\"");
2412 /* create struct cYAML root object */
2413 root = cYAML_create_object(NULL, NULL);
2417 peer_root = cYAML_create_seq(root, "peer");
2418 if (peer_root == NULL)
2422 size = count * sizeof(lnet_process_id_t);
2423 list = malloc(size);
2429 list[0].nid = libcfs_str2nid(knid);
2433 memset(&peer_info, 0, sizeof(peer_info));
2434 LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2435 peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2436 peer_info.prcfg_size = size;
2437 peer_info.prcfg_bulk = list;
2440 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_LIST,
2442 count = peer_info.prcfg_count;
2446 if (l_errno != E2BIG) {
2449 "\"cannot get peer list: %s\"",
2455 size = peer_info.prcfg_size;
2456 list = malloc(size);
2465 data = malloc(size);
2470 for (i = 0; i < count; i++) {
2472 memset(&peer_info, 0, sizeof(peer_info));
2473 LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2474 peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2475 peer_info.prcfg_prim_nid = list[i].nid;
2476 peer_info.prcfg_size = size;
2477 peer_info.prcfg_bulk = data;
2480 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_NI,
2485 if (l_errno != E2BIG) {
2488 "\"cannot get peer information: %s\"",
2494 size = peer_info.prcfg_size;
2495 data = malloc(size);
2502 peer = cYAML_create_seq_item(peer_root);
2506 if (first_seq == NULL)
2509 lnet_nid_t pnid = peer_info.prcfg_prim_nid;
2510 if (cYAML_create_string(peer, "primary nid",
2511 libcfs_nid2str(pnid))
2514 if (cYAML_create_string(peer, "Multi-Rail",
2515 peer_info.prcfg_mr ? "True" : "False")
2519 * print out the state of the peer only if details are
2523 if (cYAML_create_number(peer, "peer state",
2524 peer_info.prcfg_state)
2529 tmp = cYAML_create_seq(peer, "peer ni");
2534 for (j = 0; j < peer_info.prcfg_count; j++) {
2536 lpni_cri = (void*)nidp + sizeof(nidp);
2537 lpni_stats = (void *)lpni_cri + sizeof(*lpni_cri);
2538 msg_stats = (void *)lpni_stats + sizeof(*lpni_stats);
2539 lpni_data = (void *)msg_stats + sizeof(*msg_stats);
2541 peer_ni = cYAML_create_seq_item(tmp);
2542 if (peer_ni == NULL)
2545 if (cYAML_create_string(peer_ni, "nid",
2546 libcfs_nid2str(*nidp))
2550 if (cYAML_create_string(peer_ni, "state",
2551 lpni_cri->cr_aliveness)
2558 if (cYAML_create_number(peer_ni, "max_ni_tx_credits",
2559 lpni_cri->cr_ni_peer_tx_credits)
2563 if (cYAML_create_number(peer_ni, "available_tx_credits",
2564 lpni_cri->cr_peer_tx_credits)
2568 if (cYAML_create_number(peer_ni, "min_tx_credits",
2569 lpni_cri->cr_peer_min_tx_credits)
2573 if (cYAML_create_number(peer_ni, "tx_q_num_of_buf",
2574 lpni_cri->cr_peer_tx_qnob)
2578 if (cYAML_create_number(peer_ni, "available_rtr_credits",
2579 lpni_cri->cr_peer_rtr_credits)
2583 if (cYAML_create_number(peer_ni, "min_rtr_credits",
2584 lpni_cri->cr_peer_min_rtr_credits)
2588 if (cYAML_create_number(peer_ni, "refcount",
2589 lpni_cri->cr_refcount) == NULL)
2592 statistics = cYAML_create_object(peer_ni, "statistics");
2593 if (statistics == NULL)
2596 if (cYAML_create_number(statistics, "send_count",
2597 lpni_stats->iel_send_count)
2601 if (cYAML_create_number(statistics, "recv_count",
2602 lpni_stats->iel_recv_count)
2606 if (cYAML_create_number(statistics, "drop_count",
2607 lpni_stats->iel_drop_count)
2614 for (k = 0; k < 3; k++) {
2615 struct lnet_ioctl_comm_count *counts;
2617 msg_statistics = cYAML_create_object(peer_ni,
2618 (char *) gmsg_stat_names[k]);
2619 if (msg_statistics == NULL)
2622 counts = get_counts(msg_stats, k);
2626 if (!add_msg_stats_to_yaml_blk(msg_statistics,
2634 /* print output iff show_rc is not provided */
2635 if (show_rc == NULL)
2636 cYAML_print_tree(root);
2638 snprintf(err_str, sizeof(err_str), "\"success\"");
2639 rc = LUSTRE_CFG_RC_NO_ERR;
2644 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2645 cYAML_free_tree(root);
2646 } else if (show_rc != NULL && *show_rc != NULL) {
2647 struct cYAML *show_node;
2648 /* find the peer node, if one doesn't exist then
2649 * insert one. Otherwise add to the one there
2651 show_node = cYAML_get_object_item(*show_rc,
2653 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2654 cYAML_insert_child(show_node, first_seq);
2657 } else if (show_node == NULL) {
2658 cYAML_insert_sibling((*show_rc)->cy_child,
2662 cYAML_free_tree(root);
2668 cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
2674 int lustre_lnet_list_peer(int seq_no,
2675 struct cYAML **show_rc, struct cYAML **err_rc)
2677 struct lnet_ioctl_peer_cfg peer_info;
2678 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2683 struct cYAML *root = NULL, *list_root = NULL, *first_seq = NULL;
2684 char err_str[LNET_MAX_STR_LEN];
2685 lnet_process_id_t *list = NULL;
2687 snprintf(err_str, sizeof(err_str),
2688 "\"out of memory\"");
2690 memset(&peer_info, 0, sizeof(peer_info));
2692 /* create struct cYAML root object */
2693 root = cYAML_create_object(NULL, NULL);
2697 list_root = cYAML_create_seq(root, "peer list");
2698 if (list_root == NULL)
2702 size = count * sizeof(lnet_process_id_t);
2703 list = malloc(size);
2709 LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2710 peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2711 peer_info.prcfg_size = size;
2712 peer_info.prcfg_bulk = list;
2715 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_LIST, &peer_info);
2716 count = peer_info.prcfg_count;
2720 if (l_errno != E2BIG) {
2723 "\"cannot get peer list: %s\"",
2729 size = peer_info.prcfg_size;
2730 list = malloc(size);
2737 /* count is now the actual number of ids in the list. */
2738 for (i = 0; i < count; i++) {
2739 if (cYAML_create_string(list_root, "nid",
2740 libcfs_nid2str(list[i].nid))
2745 /* print output iff show_rc is not provided */
2746 if (show_rc == NULL)
2747 cYAML_print_tree(root);
2749 snprintf(err_str, sizeof(err_str), "\"success\"");
2750 rc = LUSTRE_CFG_RC_NO_ERR;
2755 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2756 cYAML_free_tree(root);
2757 } else if (show_rc != NULL && *show_rc != NULL) {
2758 struct cYAML *show_node;
2759 /* find the peer node, if one doesn't exist then
2760 * insert one. Otherwise add to the one there
2762 show_node = cYAML_get_object_item(*show_rc,
2764 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2765 cYAML_insert_child(show_node, first_seq);
2768 } else if (show_node == NULL) {
2769 cYAML_insert_sibling((*show_rc)->cy_child,
2773 cYAML_free_tree(root);
2779 cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
2785 static void add_to_global(struct cYAML *show_rc, struct cYAML *node,
2788 struct cYAML *show_node;
2790 show_node = cYAML_get_object_item(show_rc, "global");
2791 if (show_node != NULL)
2792 cYAML_insert_sibling(show_node->cy_child,
2795 cYAML_insert_sibling(show_rc->cy_child,
2800 static int build_global_yaml_entry(char *err_str, int err_len, int seq_no,
2801 char *name, __u32 value,
2802 struct cYAML **show_rc,
2803 struct cYAML **err_rc, int err)
2805 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2806 struct cYAML *root = NULL, *global = NULL;
2813 root = cYAML_create_object(NULL, NULL);
2817 global = cYAML_create_object(root, "global");
2821 if (cYAML_create_number(global, name,
2825 if (show_rc == NULL)
2826 cYAML_print_tree(root);
2828 snprintf(err_str, err_len, "\"success\"");
2830 rc = LUSTRE_CFG_RC_NO_ERR;
2833 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2834 cYAML_free_tree(root);
2835 } else if (show_rc != NULL && *show_rc != NULL) {
2836 add_to_global(*show_rc, global, root);
2841 cYAML_build_error(rc, seq_no, SHOW_CMD, "global", err_str, err_rc);
2846 static int ioctl_show_global_values(int ioc, int seq_no, char *name,
2847 struct cYAML **show_rc,
2848 struct cYAML **err_rc)
2850 struct lnet_ioctl_set_value data;
2853 char err_str[LNET_MAX_STR_LEN];
2855 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2857 LIBCFS_IOC_INIT_V2(data, sv_hdr);
2859 rc = l_ioctl(LNET_DEV_ID, ioc, &data);
2864 "\"cannot get %s: %s\"",
2865 name, strerror(l_errno));
2868 return build_global_yaml_entry(err_str, sizeof(err_str), seq_no, name,
2869 data.sv_value, show_rc, err_rc, l_errno);
2872 int lustre_lnet_show_max_intf(int seq_no, struct cYAML **show_rc,
2873 struct cYAML **err_rc)
2875 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2876 char val[LNET_MAX_STR_LEN];
2877 int max_intf = -1, l_errno = 0;
2878 char err_str[LNET_MAX_STR_LEN];
2880 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2882 rc = read_sysfs_file(modparam_path, "lnet_interfaces_max", val,
2886 snprintf(err_str, sizeof(err_str),
2887 "\"cannot get max interfaces: %d\"", rc);
2889 max_intf = atoi(val);
2892 return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
2893 "max_intf", max_intf, show_rc,
2897 int lustre_lnet_show_discovery(int seq_no, struct cYAML **show_rc,
2898 struct cYAML **err_rc)
2900 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2901 char val[LNET_MAX_STR_LEN];
2902 int discovery = -1, l_errno = 0;
2903 char err_str[LNET_MAX_STR_LEN];
2905 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2907 rc = read_sysfs_file(modparam_path, "lnet_peer_discovery_disabled", val,
2911 snprintf(err_str, sizeof(err_str),
2912 "\"cannot get discovery setting: %d\"", rc);
2915 * The kernel stores a discovery disabled value. User space
2916 * shows whether discovery is enabled. So the value must be
2919 discovery = !atoi(val);
2922 return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
2923 "discovery", discovery, show_rc,
2927 int lustre_lnet_show_numa_range(int seq_no, struct cYAML **show_rc,
2928 struct cYAML **err_rc)
2930 return ioctl_show_global_values(IOC_LIBCFS_GET_NUMA_RANGE, seq_no,
2931 "numa_range", show_rc, err_rc);
2934 int lustre_lnet_show_stats(int seq_no, struct cYAML **show_rc,
2935 struct cYAML **err_rc)
2937 struct lnet_ioctl_lnet_stats data;
2940 char err_str[LNET_MAX_STR_LEN];
2941 struct cYAML *root = NULL, *stats = NULL;
2943 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2945 LIBCFS_IOC_INIT_V2(data, st_hdr);
2947 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LNET_STATS, &data);
2952 "\"cannot get lnet statistics: %s\"",
2958 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2960 root = cYAML_create_object(NULL, NULL);
2964 stats = cYAML_create_object(root, "statistics");
2968 if (cYAML_create_number(stats, "msgs_alloc",
2969 data.st_cntrs.msgs_alloc) == NULL)
2972 if (cYAML_create_number(stats, "msgs_max",
2973 data.st_cntrs.msgs_max) == NULL)
2976 if (cYAML_create_number(stats, "errors",
2977 data.st_cntrs.errors) == NULL)
2980 if (cYAML_create_number(stats, "send_count",
2981 data.st_cntrs.send_count) == NULL)
2984 if (cYAML_create_number(stats, "recv_count",
2985 data.st_cntrs.recv_count) == NULL)
2988 if (cYAML_create_number(stats, "route_count",
2989 data.st_cntrs.route_count) == NULL)
2992 if (cYAML_create_number(stats, "drop_count",
2993 data.st_cntrs.drop_count) == NULL)
2996 if (cYAML_create_number(stats, "send_length",
2997 data.st_cntrs.send_length) == NULL)
3000 if (cYAML_create_number(stats, "recv_length",
3001 data.st_cntrs.recv_length) == NULL)
3004 if (cYAML_create_number(stats, "route_length",
3005 data.st_cntrs.route_length) == NULL)
3008 if (cYAML_create_number(stats, "drop_length",
3009 data.st_cntrs.drop_length) == NULL)
3012 if (show_rc == NULL)
3013 cYAML_print_tree(root);
3015 snprintf(err_str, sizeof(err_str), "\"success\"");
3016 rc = LUSTRE_CFG_RC_NO_ERR;
3018 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
3019 cYAML_free_tree(root);
3020 } else if (show_rc != NULL && *show_rc != NULL) {
3021 cYAML_insert_sibling((*show_rc)->cy_child,
3028 cYAML_build_error(rc, seq_no, SHOW_CMD, "statistics", err_str, err_rc);
3033 typedef int (*cmd_handler_t)(struct cYAML *tree,
3034 struct cYAML **show_rc,
3035 struct cYAML **err_rc);
3037 static int handle_yaml_config_route(struct cYAML *tree, struct cYAML **show_rc,
3038 struct cYAML **err_rc)
3040 struct cYAML *net, *gw, *hop, *prio, *seq_no;
3042 net = cYAML_get_object_item(tree, "net");
3043 gw = cYAML_get_object_item(tree, "gateway");
3044 hop = cYAML_get_object_item(tree, "hop");
3045 prio = cYAML_get_object_item(tree, "priority");
3046 seq_no = cYAML_get_object_item(tree, "seq_no");
3048 return lustre_lnet_config_route((net) ? net->cy_valuestring : NULL,
3049 (gw) ? gw->cy_valuestring : NULL,
3050 (hop) ? hop->cy_valueint : -1,
3051 (prio) ? prio->cy_valueint : -1,
3052 (seq_no) ? seq_no->cy_valueint : -1,
3056 static void yaml_free_string_array(char **array, int num)
3059 char **sub_array = array;
3061 for (i = 0; i < num; i++) {
3062 if (*sub_array != NULL)
3072 * 0: <intf_name>['['<expr>']']
3073 * 1: <intf_name>['['<expr>']']
3075 static int yaml_copy_intf_info(struct cYAML *intf_tree,
3076 struct lnet_dlc_network_descr *nw_descr)
3078 struct cYAML *child = NULL;
3079 int intf_num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
3080 struct lnet_dlc_intf_descr *intf_descr, *tmp;
3082 if (intf_tree == NULL || nw_descr == NULL)
3083 return LUSTRE_CFG_RC_BAD_PARAM;
3085 /* now grab all the interfaces and their cpts */
3086 child = intf_tree->cy_child;
3087 while (child != NULL) {
3088 if (child->cy_valuestring == NULL) {
3089 child = child->cy_next;
3093 if (strlen(child->cy_valuestring) >= LNET_MAX_STR_LEN)
3096 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist,
3097 child->cy_valuestring,
3098 strlen(child->cy_valuestring));
3099 if (rc != LUSTRE_CFG_RC_NO_ERR)
3103 child = child->cy_next;
3107 return LUSTRE_CFG_RC_MISSING_PARAM;
3112 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
3114 list_del(&intf_descr->intf_on_network);
3115 free_intf_descr(intf_descr);
3122 yaml_extract_cmn_tunables(struct cYAML *tree,
3123 struct lnet_ioctl_config_lnd_cmn_tunables *tunables,
3124 struct cfs_expr_list **global_cpts)
3126 struct cYAML *tun, *item, *smp;
3129 tun = cYAML_get_object_item(tree, "tunables");
3131 item = cYAML_get_object_item(tun, "peer_timeout");
3133 tunables->lct_peer_timeout = item->cy_valueint;
3134 item = cYAML_get_object_item(tun, "peer_credits");
3136 tunables->lct_peer_tx_credits = item->cy_valueint;
3137 item = cYAML_get_object_item(tun, "peer_buffer_credits");
3139 tunables->lct_peer_rtr_credits = item->cy_valueint;
3140 item = cYAML_get_object_item(tun, "credits");
3142 tunables->lct_max_tx_credits = item->cy_valueint;
3143 smp = cYAML_get_object_item(tun, "CPT");
3145 rc = cfs_expr_list_parse(smp->cy_valuestring,
3146 strlen(smp->cy_valuestring),
3147 0, UINT_MAX, global_cpts);
3149 *global_cpts = NULL;
3159 yaml_extract_tunables(struct cYAML *tree,
3160 struct lnet_ioctl_config_lnd_tunables *tunables,
3161 struct cfs_expr_list **global_cpts,
3166 rc = yaml_extract_cmn_tunables(tree, &tunables->lt_cmn,
3172 lustre_yaml_extract_lnd_tunables(tree, net_type,
3180 * - net type: <net>[<NUM>]
3182 * - nid: <ip>@<net>[<NUM>]
3185 * 0: <intf_name>['['<expr>']']
3186 * 1: <intf_name>['['<expr>']']
3188 * peer_timeout: <NUM>
3189 * peer_credits: <NUM>
3190 * peer_buffer_credits: <NUM>
3193 * peercredits_hiw: <NUM>
3194 * map_on_demand: <NUM>
3195 * concurrent_sends: <NUM>
3196 * fmr_pool_size: <NUM>
3197 * fmr_flush_trigger: <NUM>
3200 * At least one interface is required. If no interfaces are provided the
3201 * network interface can not be configured.
3203 static int handle_yaml_config_ni(struct cYAML *tree, struct cYAML **show_rc,
3204 struct cYAML **err_rc)
3206 struct cYAML *net, *intf, *seq_no, *ip2net = NULL, *local_nis = NULL,
3208 int num_entries = 0, rc;
3209 struct lnet_dlc_network_descr nw_descr;
3210 struct cfs_expr_list *global_cpts = NULL;
3211 struct lnet_ioctl_config_lnd_tunables tunables;
3214 memset(&tunables, 0, sizeof(tunables));
3216 INIT_LIST_HEAD(&nw_descr.network_on_rule);
3217 INIT_LIST_HEAD(&nw_descr.nw_intflist);
3219 ip2net = cYAML_get_object_item(tree, "ip2net");
3220 net = cYAML_get_object_item(tree, "net type");
3222 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
3224 nw_descr.nw_id = LOLND;
3227 * if neither net nor ip2nets are present, then we can not
3228 * configure the network.
3230 if (!net && !ip2net)
3231 return LUSTRE_CFG_RC_MISSING_PARAM;
3233 local_nis = cYAML_get_object_item(tree, "local NI(s)");
3234 if (local_nis == NULL)
3235 return LUSTRE_CFG_RC_MISSING_PARAM;
3237 if (!cYAML_is_sequence(local_nis))
3238 return LUSTRE_CFG_RC_BAD_PARAM;
3240 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
3241 intf = cYAML_get_object_item(item, "interfaces");
3244 num_entries = yaml_copy_intf_info(intf, &nw_descr);
3245 if (num_entries <= 0) {
3246 cYAML_build_error(num_entries, -1, "ni", "add",
3247 "bad interface list",
3249 return LUSTRE_CFG_RC_BAD_PARAM;
3253 found = yaml_extract_tunables(tree, &tunables, &global_cpts,
3254 LNET_NETTYP(nw_descr.nw_id));
3255 seq_no = cYAML_get_object_item(tree, "seq_no");
3257 rc = lustre_lnet_config_ni(&nw_descr,
3259 (ip2net) ? ip2net->cy_valuestring : NULL,
3260 (found) ? &tunables: NULL,
3261 (seq_no) ? seq_no->cy_valueint : -1,
3264 if (global_cpts != NULL)
3265 cfs_expr_list_free(global_cpts);
3272 * - net-spec: <tcp|o2ib|gni>[NUM]
3274 * 0: <intf name>['['<expr>']']
3275 * 1: <intf name>['['<expr>']']
3277 * 0: <expr.expr.expr.expr>
3278 * 1: <expr.expr.expr.expr>
3280 static int handle_yaml_config_ip2nets(struct cYAML *tree,
3281 struct cYAML **show_rc,
3282 struct cYAML **err_rc)
3284 struct cYAML *net, *ip_range, *item = NULL, *intf = NULL,
3286 struct lustre_lnet_ip2nets ip2nets;
3287 struct lustre_lnet_ip_range_descr *ip_range_descr = NULL,
3289 int rc = LUSTRE_CFG_RC_NO_ERR;
3290 struct cfs_expr_list *global_cpts = NULL;
3291 struct cfs_expr_list *el, *el_tmp;
3292 struct lnet_ioctl_config_lnd_tunables tunables;
3293 struct lnet_dlc_intf_descr *intf_descr, *intf_tmp;
3296 memset(&tunables, 0, sizeof(tunables));
3298 /* initialize all lists */
3299 INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
3300 INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
3301 INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
3303 net = cYAML_get_object_item(tree, "net-spec");
3305 return LUSTRE_CFG_RC_BAD_PARAM;
3307 if (net != NULL && net->cy_valuestring == NULL)
3308 return LUSTRE_CFG_RC_BAD_PARAM;
3310 /* assign the network id */
3311 ip2nets.ip2nets_net.nw_id = libcfs_str2net(net->cy_valuestring);
3312 if (ip2nets.ip2nets_net.nw_id == LNET_NID_ANY)
3313 return LUSTRE_CFG_RC_BAD_PARAM;
3315 seq_no = cYAML_get_object_item(tree, "seq_no");
3317 intf = cYAML_get_object_item(tree, "interfaces");
3319 rc = yaml_copy_intf_info(intf, &ip2nets.ip2nets_net);
3321 return LUSTRE_CFG_RC_BAD_PARAM;
3324 ip_range = cYAML_get_object_item(tree, "ip-range");
3325 if (ip_range != NULL) {
3326 item = ip_range->cy_child;
3327 while (item != NULL) {
3328 if (item->cy_valuestring == NULL) {
3329 item = item->cy_next;
3333 rc = lustre_lnet_add_ip_range(&ip2nets.ip2nets_ip_ranges,
3334 item->cy_valuestring);
3336 if (rc != LUSTRE_CFG_RC_NO_ERR)
3339 item = item->cy_next;
3343 found = yaml_extract_tunables(tree, &tunables, &global_cpts,
3344 LNET_NETTYP(ip2nets.ip2nets_net.nw_id));
3346 rc = lustre_lnet_config_ip2nets(&ip2nets,
3347 (found) ? &tunables : NULL,
3349 (seq_no) ? seq_no->cy_valueint : -1,
3353 * don't stop because there was no match. Continue processing the
3354 * rest of the rules. If non-match then nothing is configured
3356 if (rc == LUSTRE_CFG_RC_NO_MATCH)
3357 rc = LUSTRE_CFG_RC_NO_ERR;
3359 list_for_each_entry_safe(intf_descr, intf_tmp,
3360 &ip2nets.ip2nets_net.nw_intflist,
3362 list_del(&intf_descr->intf_on_network);
3363 free_intf_descr(intf_descr);
3366 list_for_each_entry_safe(ip_range_descr, tmp,
3367 &ip2nets.ip2nets_ip_ranges,
3369 list_del(&ip_range_descr->ipr_entry);
3370 list_for_each_entry_safe(el, el_tmp, &ip_range_descr->ipr_expr,
3372 list_del(&el->el_link);
3373 cfs_expr_list_free(el);
3375 free(ip_range_descr);
3381 static int handle_yaml_del_ni(struct cYAML *tree, struct cYAML **show_rc,
3382 struct cYAML **err_rc)
3384 struct cYAML *net = NULL, *intf = NULL, *seq_no = NULL, *item = NULL,
3386 int num_entries, rc;
3387 struct lnet_dlc_network_descr nw_descr;
3389 INIT_LIST_HEAD(&nw_descr.network_on_rule);
3390 INIT_LIST_HEAD(&nw_descr.nw_intflist);
3392 net = cYAML_get_object_item(tree, "net type");
3394 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
3396 local_nis = cYAML_get_object_item(tree, "local NI(s)");
3397 if (local_nis == NULL)
3398 return LUSTRE_CFG_RC_MISSING_PARAM;
3400 if (!cYAML_is_sequence(local_nis))
3401 return LUSTRE_CFG_RC_BAD_PARAM;
3403 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
3404 intf = cYAML_get_object_item(item, "interfaces");
3407 num_entries = yaml_copy_intf_info(intf, &nw_descr);
3408 if (num_entries <= 0) {
3409 cYAML_build_error(num_entries, -1, "ni", "add",
3410 "bad interface list",
3412 return LUSTRE_CFG_RC_BAD_PARAM;
3416 seq_no = cYAML_get_object_item(tree, "seq_no");
3418 rc = lustre_lnet_del_ni((net) ? &nw_descr : NULL,
3419 (seq_no) ? seq_no->cy_valueint : -1,
3425 static int yaml_copy_peer_nids(struct cYAML *tree, char ***nidsppp, bool del)
3427 struct cYAML *nids_entry = NULL, *child = NULL, *entry = NULL,
3430 int num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
3432 prim_nid = cYAML_get_object_item(tree, "primary nid");
3433 if (!prim_nid || !prim_nid->cy_valuestring)
3434 return LUSTRE_CFG_RC_MISSING_PARAM;
3436 nids_entry = cYAML_get_object_item(tree, "peer ni");
3437 if (cYAML_is_sequence(nids_entry)) {
3438 while (cYAML_get_next_seq_item(nids_entry, &child)) {
3439 entry = cYAML_get_object_item(child, "nid");
3440 /* don't count an empty entry */
3441 if (!entry || !entry->cy_valuestring)
3444 if ((strcmp(entry->cy_valuestring, prim_nid->cy_valuestring)
3447 * primary nid is present in the list of
3448 * nids so that means we want to delete
3449 * the entire peer, so no need to go
3450 * further. Just delete the entire peer.
3460 return LUSTRE_CFG_RC_MISSING_PARAM;
3462 nids = calloc(sizeof(*nids) * num, 1);
3464 return LUSTRE_CFG_RC_OUT_OF_MEM;
3466 /* now grab all the nids */
3469 while (cYAML_get_next_seq_item(nids_entry, &child)) {
3470 entry = cYAML_get_object_item(child, "nid");
3471 if (!entry || !entry->cy_valuestring)
3474 nids[num] = calloc(strlen(entry->cy_valuestring) + 1, 1);
3476 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3479 strncpy(nids[num], entry->cy_valuestring,
3480 strlen(entry->cy_valuestring));
3490 yaml_free_string_array(nids, num);
3495 static int handle_yaml_config_peer(struct cYAML *tree, struct cYAML **show_rc,
3496 struct cYAML **err_rc)
3500 struct cYAML *seq_no, *prim_nid, *non_mr;
3502 num = yaml_copy_peer_nids(tree, &nids, false);
3506 seq_no = cYAML_get_object_item(tree, "seq_no");
3507 prim_nid = cYAML_get_object_item(tree, "primary nid");
3508 non_mr = cYAML_get_object_item(tree, "non_mr");
3510 rc = lustre_lnet_config_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
3512 (non_mr) ? false : true,
3513 (seq_no) ? seq_no->cy_valueint : -1,
3516 yaml_free_string_array(nids, num);
3520 static int handle_yaml_del_peer(struct cYAML *tree, struct cYAML **show_rc,
3521 struct cYAML **err_rc)
3525 struct cYAML *seq_no, *prim_nid;
3527 num = yaml_copy_peer_nids(tree, &nids, true);
3531 seq_no = cYAML_get_object_item(tree, "seq_no");
3532 prim_nid = cYAML_get_object_item(tree, "primary nid");
3534 rc = lustre_lnet_del_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
3536 (seq_no) ? seq_no->cy_valueint : -1,
3539 yaml_free_string_array(nids, num);
3543 static int handle_yaml_config_buffers(struct cYAML *tree,
3544 struct cYAML **show_rc,
3545 struct cYAML **err_rc)
3548 struct cYAML *tiny, *small, *large, *seq_no;
3550 tiny = cYAML_get_object_item(tree, "tiny");
3551 small = cYAML_get_object_item(tree, "small");
3552 large = cYAML_get_object_item(tree, "large");
3553 seq_no = cYAML_get_object_item(tree, "seq_no");
3555 rc = lustre_lnet_config_buffers((tiny) ? tiny->cy_valueint : -1,
3556 (small) ? small->cy_valueint : -1,
3557 (large) ? large->cy_valueint : -1,
3558 (seq_no) ? seq_no->cy_valueint : -1,
3564 static int handle_yaml_config_routing(struct cYAML *tree,
3565 struct cYAML **show_rc,
3566 struct cYAML **err_rc)
3568 int rc = LUSTRE_CFG_RC_NO_ERR;
3569 struct cYAML *seq_no, *enable;
3571 seq_no = cYAML_get_object_item(tree, "seq_no");
3572 enable = cYAML_get_object_item(tree, "enable");
3575 rc = lustre_lnet_enable_routing(enable->cy_valueint,
3577 seq_no->cy_valueint : -1,
3584 static int handle_yaml_del_route(struct cYAML *tree, struct cYAML **show_rc,
3585 struct cYAML **err_rc)
3589 struct cYAML *seq_no;
3591 net = cYAML_get_object_item(tree, "net");
3592 gw = cYAML_get_object_item(tree, "gateway");
3593 seq_no = cYAML_get_object_item(tree, "seq_no");
3595 return lustre_lnet_del_route((net) ? net->cy_valuestring : NULL,
3596 (gw) ? gw->cy_valuestring : NULL,
3597 (seq_no) ? seq_no->cy_valueint : -1,
3601 static int handle_yaml_del_routing(struct cYAML *tree, struct cYAML **show_rc,
3602 struct cYAML **err_rc)
3604 struct cYAML *seq_no;
3606 seq_no = cYAML_get_object_item(tree, "seq_no");
3608 return lustre_lnet_enable_routing(0, (seq_no) ?
3609 seq_no->cy_valueint : -1,
3613 static int handle_yaml_show_route(struct cYAML *tree, struct cYAML **show_rc,
3614 struct cYAML **err_rc)
3620 struct cYAML *detail;
3621 struct cYAML *seq_no;
3623 net = cYAML_get_object_item(tree, "net");
3624 gw = cYAML_get_object_item(tree, "gateway");
3625 hop = cYAML_get_object_item(tree, "hop");
3626 prio = cYAML_get_object_item(tree, "priority");
3627 detail = cYAML_get_object_item(tree, "detail");
3628 seq_no = cYAML_get_object_item(tree, "seq_no");
3630 return lustre_lnet_show_route((net) ? net->cy_valuestring : NULL,
3631 (gw) ? gw->cy_valuestring : NULL,
3632 (hop) ? hop->cy_valueint : -1,
3633 (prio) ? prio->cy_valueint : -1,
3634 (detail) ? detail->cy_valueint : 0,
3635 (seq_no) ? seq_no->cy_valueint : -1,
3640 static int handle_yaml_show_net(struct cYAML *tree, struct cYAML **show_rc,
3641 struct cYAML **err_rc)
3643 struct cYAML *net, *detail, *seq_no;
3645 net = cYAML_get_object_item(tree, "net");
3646 detail = cYAML_get_object_item(tree, "detail");
3647 seq_no = cYAML_get_object_item(tree, "seq_no");
3649 return lustre_lnet_show_net((net) ? net->cy_valuestring : NULL,
3650 (detail) ? detail->cy_valueint : 0,
3651 (seq_no) ? seq_no->cy_valueint : -1,
3656 static int handle_yaml_show_routing(struct cYAML *tree, struct cYAML **show_rc,
3657 struct cYAML **err_rc)
3659 struct cYAML *seq_no;
3661 seq_no = cYAML_get_object_item(tree, "seq_no");
3663 return lustre_lnet_show_routing((seq_no) ? seq_no->cy_valueint : -1,
3667 static int handle_yaml_show_peers(struct cYAML *tree, struct cYAML **show_rc,
3668 struct cYAML **err_rc)
3670 struct cYAML *seq_no, *nid, *detail;
3672 seq_no = cYAML_get_object_item(tree, "seq_no");
3673 detail = cYAML_get_object_item(tree, "detail");
3674 nid = cYAML_get_object_item(tree, "nid");
3676 return lustre_lnet_show_peer((nid) ? nid->cy_valuestring : NULL,
3677 (detail) ? detail->cy_valueint : 0,
3678 (seq_no) ? seq_no->cy_valueint : -1,
3682 static int handle_yaml_show_stats(struct cYAML *tree, struct cYAML **show_rc,
3683 struct cYAML **err_rc)
3685 struct cYAML *seq_no;
3687 seq_no = cYAML_get_object_item(tree, "seq_no");
3689 return lustre_lnet_show_stats((seq_no) ? seq_no->cy_valueint : -1,
3693 static int handle_yaml_config_global_settings(struct cYAML *tree,
3694 struct cYAML **show_rc,
3695 struct cYAML **err_rc)
3697 struct cYAML *max_intf, *numa, *discovery, *seq_no;
3700 seq_no = cYAML_get_object_item(tree, "seq_no");
3701 max_intf = cYAML_get_object_item(tree, "max_intf");
3703 rc = lustre_lnet_config_max_intf(max_intf->cy_valueint,
3704 seq_no ? seq_no->cy_valueint
3708 numa = cYAML_get_object_item(tree, "numa_range");
3710 rc = lustre_lnet_config_numa_range(numa->cy_valueint,
3711 seq_no ? seq_no->cy_valueint
3715 discovery = cYAML_get_object_item(tree, "discovery");
3717 rc = lustre_lnet_config_discovery(discovery->cy_valueint,
3718 seq_no ? seq_no->cy_valueint
3725 static int handle_yaml_del_global_settings(struct cYAML *tree,
3726 struct cYAML **show_rc,
3727 struct cYAML **err_rc)
3729 struct cYAML *max_intf, *numa, *discovery, *seq_no;
3732 seq_no = cYAML_get_object_item(tree, "seq_no");
3733 max_intf = cYAML_get_object_item(tree, "max_intf");
3735 rc = lustre_lnet_config_max_intf(LNET_INTERFACES_MAX_DEFAULT,
3736 seq_no ? seq_no->cy_valueint
3740 numa = cYAML_get_object_item(tree, "numa_range");
3742 rc = lustre_lnet_config_numa_range(0,
3743 seq_no ? seq_no->cy_valueint
3747 /* peer discovery is enabled by default */
3748 discovery = cYAML_get_object_item(tree, "discovery");
3750 rc = lustre_lnet_config_discovery(1,
3751 seq_no ? seq_no->cy_valueint
3758 static int handle_yaml_show_global_settings(struct cYAML *tree,
3759 struct cYAML **show_rc,
3760 struct cYAML **err_rc)
3762 struct cYAML *max_intf, *numa, *discovery, *seq_no;
3765 seq_no = cYAML_get_object_item(tree, "seq_no");
3766 max_intf = cYAML_get_object_item(tree, "max_intf");
3768 rc = lustre_lnet_show_max_intf(seq_no ? seq_no->cy_valueint
3772 numa = cYAML_get_object_item(tree, "numa_range");
3774 rc = lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint
3778 discovery = cYAML_get_object_item(tree, "discovery");
3780 rc = lustre_lnet_show_discovery(seq_no ? seq_no->cy_valueint
3787 static int handle_yaml_ping(struct cYAML *tree, struct cYAML **show_rc,
3788 struct cYAML **err_rc)
3790 struct cYAML *seq_no, *nid, *timeout;
3792 seq_no = cYAML_get_object_item(tree, "seq_no");
3793 nid = cYAML_get_object_item(tree, "primary nid");
3794 timeout = cYAML_get_object_item(tree, "timeout");
3796 return lustre_lnet_ping_nid((nid) ? nid->cy_valuestring : NULL,
3797 (timeout) ? timeout->cy_valueint : 1000,
3798 (seq_no) ? seq_no->cy_valueint : -1,
3802 static int handle_yaml_discover(struct cYAML *tree, struct cYAML **show_rc,
3803 struct cYAML **err_rc)
3805 struct cYAML *seq_no, *nid, *force;
3807 seq_no = cYAML_get_object_item(tree, "seq_no");
3808 nid = cYAML_get_object_item(tree, "primary nid");
3809 force = cYAML_get_object_item(tree, "force");
3811 return lustre_lnet_discover_nid((nid) ? nid->cy_valuestring : NULL,
3812 (force) ? force->cy_valueint : 0,
3813 (seq_no) ? seq_no->cy_valueint : -1,
3817 static int handle_yaml_no_op()
3819 return LUSTRE_CFG_RC_NO_ERR;
3822 struct lookup_cmd_hdlr_tbl {
3827 static struct lookup_cmd_hdlr_tbl lookup_config_tbl[] = {
3828 { .name = "route", .cb = handle_yaml_config_route },
3829 { .name = "net", .cb = handle_yaml_config_ni },
3830 { .name = "ip2nets", .cb = handle_yaml_config_ip2nets },
3831 { .name = "peer", .cb = handle_yaml_config_peer },
3832 { .name = "routing", .cb = handle_yaml_config_routing },
3833 { .name = "buffers", .cb = handle_yaml_config_buffers },
3834 { .name = "statistics", .cb = handle_yaml_no_op },
3835 { .name = "global", .cb = handle_yaml_config_global_settings},
3836 { .name = "ping", .cb = handle_yaml_no_op },
3837 { .name = "discover", .cb = handle_yaml_no_op },
3840 static struct lookup_cmd_hdlr_tbl lookup_del_tbl[] = {
3841 { .name = "route", .cb = handle_yaml_del_route },
3842 { .name = "net", .cb = handle_yaml_del_ni },
3843 { .name = "ip2nets", .cb = handle_yaml_no_op },
3844 { .name = "peer", .cb = handle_yaml_del_peer },
3845 { .name = "routing", .cb = handle_yaml_del_routing },
3846 { .name = "buffers", .cb = handle_yaml_no_op },
3847 { .name = "statistics", .cb = handle_yaml_no_op },
3848 { .name = "global", .cb = handle_yaml_del_global_settings},
3849 { .name = "ping", .cb = handle_yaml_no_op },
3850 { .name = "discover", .cb = handle_yaml_no_op },
3853 static struct lookup_cmd_hdlr_tbl lookup_show_tbl[] = {
3854 { .name = "route", .cb = handle_yaml_show_route },
3855 { .name = "net", .cb = handle_yaml_show_net },
3856 { .name = "peer", .cb = handle_yaml_show_peers },
3857 { .name = "ip2nets", .cb = handle_yaml_no_op },
3858 { .name = "routing", .cb = handle_yaml_show_routing },
3859 { .name = "buffers", .cb = handle_yaml_show_routing },
3860 { .name = "statistics", .cb = handle_yaml_show_stats },
3861 { .name = "global", .cb = handle_yaml_show_global_settings},
3862 { .name = "ping", .cb = handle_yaml_no_op },
3863 { .name = "discover", .cb = handle_yaml_no_op },
3866 static struct lookup_cmd_hdlr_tbl lookup_exec_tbl[] = {
3867 { .name = "route", .cb = handle_yaml_no_op },
3868 { .name = "net", .cb = handle_yaml_no_op },
3869 { .name = "peer", .cb = handle_yaml_no_op },
3870 { .name = "ip2nets", .cb = handle_yaml_no_op },
3871 { .name = "routing", .cb = handle_yaml_no_op },
3872 { .name = "buffers", .cb = handle_yaml_no_op },
3873 { .name = "statistics", .cb = handle_yaml_no_op },
3874 { .name = "global", .cb = handle_yaml_no_op },
3875 { .name = "ping", .cb = handle_yaml_ping },
3876 { .name = "discover", .cb = handle_yaml_discover },
3879 static cmd_handler_t lookup_fn(char *key,
3880 struct lookup_cmd_hdlr_tbl *tbl)
3886 for (i = 0; tbl[i].name != NULL; i++) {
3887 if (strncmp(key, tbl[i].name, strlen(tbl[i].name)) == 0)
3894 static int lustre_yaml_cb_helper(char *f, struct lookup_cmd_hdlr_tbl *table,
3895 struct cYAML **show_rc, struct cYAML **err_rc)
3897 struct cYAML *tree, *item = NULL, *head, *child;
3899 char err_str[LNET_MAX_STR_LEN];
3900 int rc = LUSTRE_CFG_RC_NO_ERR, return_rc = LUSTRE_CFG_RC_NO_ERR;
3902 tree = cYAML_build_tree(f, NULL, 0, err_rc, false);
3904 return LUSTRE_CFG_RC_BAD_PARAM;
3906 child = tree->cy_child;
3907 while (child != NULL) {
3908 cb = lookup_fn(child->cy_string, table);
3910 snprintf(err_str, sizeof(err_str),
3911 "\"call back for '%s' not found\"",
3913 cYAML_build_error(LUSTRE_CFG_RC_BAD_PARAM, -1,
3914 "yaml", "helper", err_str, err_rc);
3918 if (cYAML_is_sequence(child)) {
3919 while ((head = cYAML_get_next_seq_item(child, &item))
3921 rc = cb(head, show_rc, err_rc);
3922 if (rc != LUSTRE_CFG_RC_NO_ERR)
3926 rc = cb(child, show_rc, err_rc);
3927 if (rc != LUSTRE_CFG_RC_NO_ERR)
3931 child = child->cy_next;
3935 cYAML_free_tree(tree);
3940 int lustre_yaml_config(char *f, struct cYAML **err_rc)
3942 return lustre_yaml_cb_helper(f, lookup_config_tbl,
3946 int lustre_yaml_del(char *f, struct cYAML **err_rc)
3948 return lustre_yaml_cb_helper(f, lookup_del_tbl,
3952 int lustre_yaml_show(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
3954 return lustre_yaml_cb_helper(f, lookup_show_tbl,
3958 int lustre_yaml_exec(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
3960 return lustre_yaml_cb_helper(f, lookup_exec_tbl,