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, 2017, 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"
55 #define CONFIG_CMD "configure"
56 #define UNCONFIG_CMD "unconfigure"
59 #define SHOW_CMD "show"
61 #define MANAGE_CMD "manage"
63 #define modparam_path "/sys/module/lnet/parameters/"
64 #define gni_nid_path "/proc/cray_xt/"
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);
403 void lustre_lnet_config_lib_uninit(void)
405 unregister_ioc_dev(LNET_DEV_ID);
408 int lustre_lnet_config_ni_system(bool up, bool load_ni_from_mod,
409 int seq_no, struct cYAML **err_rc)
411 struct libcfs_ioctl_data data;
414 char err_str[LNET_MAX_STR_LEN];
416 snprintf(err_str, sizeof(err_str), "\"Success\"");
418 LIBCFS_IOC_INIT(data);
420 /* Reverse logic is used here in order not to change
421 * the lctl utility */
422 data.ioc_flags = load_ni_from_mod ? 0 : 1;
424 opc = up ? IOC_LIBCFS_CONFIGURE : IOC_LIBCFS_UNCONFIGURE;
426 rc = l_ioctl(LNET_DEV_ID, opc, &data);
430 "\"LNet %s error: %s\"", (up) ? "configure" :
431 "unconfigure", strerror(errno));
435 cYAML_build_error(rc, seq_no, (up) ? CONFIG_CMD : UNCONFIG_CMD,
436 "lnet", err_str, err_rc);
441 static lnet_nid_t *allocate_create_nid_array(char **nids, __u32 num_nids,
444 lnet_nid_t *array = NULL;
447 if (!nids || num_nids == 0) {
448 snprintf(err_str, LNET_MAX_STR_LEN, "no NIDs to add");
452 array = calloc(sizeof(*array) * num_nids, 1);
454 snprintf(err_str, LNET_MAX_STR_LEN, "out of memory");
458 for (i = 0; i < num_nids; i++) {
459 array[i] = libcfs_str2nid(nids[i]);
460 if (array[i] == LNET_NID_ANY) {
462 snprintf(err_str, LNET_MAX_STR_LEN,
472 static int dispatch_peer_ni_cmd(lnet_nid_t pnid, lnet_nid_t nid, __u32 cmd,
473 struct lnet_ioctl_peer_cfg *data,
474 char *err_str, char *cmd_str)
478 data->prcfg_prim_nid = pnid;
479 data->prcfg_cfg_nid = nid;
481 rc = l_ioctl(LNET_DEV_ID, cmd, data);
486 "\"cannot %s peer ni: %s\"",
487 (cmd_str) ? cmd_str : "add", strerror(errno));
493 static int infra_ping_nid(char *ping_nids, char *oper, int param, int ioc_call,
494 int seq_no, struct cYAML **show_rc,
495 struct cYAML **err_rc)
498 struct lnet_ioctl_ping_data ping;
499 struct cYAML *root = NULL, *ping_node = NULL, *item = NULL,
500 *first_seq = NULL, *tmp = NULL, *peer_ni = NULL;
501 struct lnet_process_id id;
502 char err_str[LNET_MAX_STR_LEN] = {0};
503 char *sep, *token, *end;
506 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
510 len = (sizeof(struct lnet_process_id) * LNET_INTERFACES_MAX_DEFAULT);
512 data = calloc(1, len);
516 /* create struct cYAML root object */
517 root = cYAML_create_object(NULL, NULL);
521 ping_node = cYAML_create_seq(root, oper);
522 if (ping_node == NULL)
525 /* tokenise each nid in string ping_nids */
526 token = strtok(ping_nids, ",");
529 item = cYAML_create_seq_item(ping_node);
533 if (first_seq == NULL)
536 /* check if '-' is a part of NID, token */
537 sep = strchr(token, '-');
539 id.pid = LNET_PID_ANY;
540 /* if no net is specified, libcfs_str2nid() will assume tcp */
541 id.nid = libcfs_str2nid(token);
542 if (id.nid == LNET_NID_ANY) {
543 snprintf(err_str, sizeof(err_str),
544 "\"cannot parse NID '%s'\"",
546 rc = LUSTRE_CFG_RC_BAD_PARAM;
547 cYAML_build_error(rc, seq_no, MANAGE_CMD,
548 oper, err_str, err_rc);
552 if (token[0] == 'u' || token[0] == 'U')
553 id.pid = (strtoul(&token[1], &end, 0) |
554 (LNET_PID_USERFLAG));
556 id.pid = strtoul(token, &end, 0);
558 /* assuming '-' is part of hostname */
560 id.pid = LNET_PID_ANY;
561 id.nid = libcfs_str2nid(token);
562 if (id.nid == LNET_NID_ANY) {
563 snprintf(err_str, sizeof(err_str),
564 "\"cannot parse NID '%s'\"",
566 rc = LUSTRE_CFG_RC_BAD_PARAM;
567 cYAML_build_error(rc, seq_no, MANAGE_CMD,
573 id.nid = libcfs_str2nid(sep + 1);
574 if (id.nid == LNET_NID_ANY) {
575 snprintf(err_str, sizeof(err_str),
576 "\"cannot parse NID '%s'\"",
578 rc = LUSTRE_CFG_RC_BAD_PARAM;
579 cYAML_build_error(rc, seq_no, MANAGE_CMD,
586 LIBCFS_IOC_INIT_V2(ping, ping_hdr);
587 ping.ping_hdr.ioc_len = sizeof(ping);
589 ping.op_param = param;
590 ping.ping_count = LNET_INTERFACES_MAX_DEFAULT;
591 ping.ping_buf = data;
593 rc = l_ioctl(LNET_DEV_ID, ioc_call, &ping);
596 sizeof(err_str), "failed to %s %s: %s\n", oper,
597 id.pid == LNET_PID_ANY ?
598 libcfs_nid2str(id.nid) :
599 libcfs_id2str(id), strerror(errno));
600 rc = LUSTRE_CFG_RC_BAD_PARAM;
601 cYAML_build_error(rc, seq_no, MANAGE_CMD,
602 oper, err_str, err_rc);
606 if (cYAML_create_string(item, "primary nid",
607 libcfs_nid2str(ping.ping_id.nid)) == NULL)
610 if (cYAML_create_string(item, "Multi-Rail", ping.mr_info ?
611 "True" : "False") == NULL)
614 tmp = cYAML_create_seq(item, "peer ni");
618 for (i = 0; i < ping.ping_count; i++) {
619 if (!strcmp(libcfs_nid2str(ping.ping_buf[i].nid),
622 peer_ni = cYAML_create_seq_item(tmp);
625 memset(buf, 0, sizeof buf);
626 snprintf(buf, sizeof buf, "nid");
627 if (cYAML_create_string(peer_ni, buf,
628 libcfs_nid2str(ping.ping_buf[i].nid)) == NULL)
634 } while ((token = strtok(NULL, ",")) != NULL);
637 rc = LUSTRE_CFG_RC_NO_ERR;
642 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
643 cYAML_free_tree(root);
644 } else if (show_rc != NULL && *show_rc != NULL) {
645 struct cYAML *show_node;
646 show_node = cYAML_get_object_item(*show_rc, oper);
647 if (show_node != NULL && cYAML_is_sequence(show_node)) {
648 cYAML_insert_child(show_node, first_seq);
651 } else if (show_node == NULL) {
652 cYAML_insert_sibling((*show_rc)->cy_child,
656 cYAML_free_tree(root);
665 int lustre_lnet_ping_nid(char *ping_nids, int timeout, int seq_no,
666 struct cYAML **show_rc, struct cYAML **err_rc)
670 rc = infra_ping_nid(ping_nids, "ping", timeout, IOC_LIBCFS_PING_PEER,
671 seq_no, show_rc, err_rc);
675 int lustre_lnet_discover_nid(char *ping_nids, int force, int seq_no,
676 struct cYAML **show_rc, struct cYAML **err_rc)
680 rc = infra_ping_nid(ping_nids, "discover", force, IOC_LIBCFS_DISCOVER,
681 seq_no, show_rc, err_rc);
685 int lustre_lnet_config_peer_nid(char *pnid, char **nid, int num_nids,
686 bool mr, int seq_no, struct cYAML **err_rc)
688 struct lnet_ioctl_peer_cfg data;
689 lnet_nid_t prim_nid = LNET_NID_ANY;
690 int rc = LUSTRE_CFG_RC_NO_ERR;
692 bool nid0_used = false;
693 char err_str[LNET_MAX_STR_LEN] = {0};
694 lnet_nid_t *nids = allocate_create_nid_array(nid, num_nids, err_str);
697 prim_nid = libcfs_str2nid(pnid);
698 if (prim_nid == LNET_NID_ANY) {
699 snprintf(err_str, sizeof(err_str),
702 rc = LUSTRE_CFG_RC_MISSING_PARAM;
705 } else if (!nids || nids[0] == LNET_NID_ANY) {
706 snprintf(err_str, sizeof(err_str),
707 "no NIDs provided for configuration");
708 rc = LUSTRE_CFG_RC_MISSING_PARAM;
711 prim_nid = LNET_NID_ANY;
714 snprintf(err_str, sizeof(err_str), "\"Success\"");
716 LIBCFS_IOC_INIT_V2(data, prcfg_hdr);
720 * if prim_nid is not specified use the first nid in the list of
721 * nids provided as the prim_nid. NOTE: on entering 'if' we must
722 * have at least 1 NID
724 if (prim_nid == LNET_NID_ANY) {
729 /* Create the prim_nid first */
730 rc = dispatch_peer_ni_cmd(prim_nid, LNET_NID_ANY,
731 IOC_LIBCFS_ADD_PEER_NI,
732 &data, err_str, "add");
737 /* add the rest of the nids to the key nid if any are available */
738 for (idx = nid0_used ? 1 : 0 ; nids && idx < num_nids; idx++) {
740 * If prim_nid is not provided then the first nid in the
741 * list becomes the prim_nid. First time round the loop use
742 * LNET_NID_ANY for the first parameter, then use nid[0]
743 * as the key nid after wards
745 rc = dispatch_peer_ni_cmd(prim_nid, nids[idx],
746 IOC_LIBCFS_ADD_PEER_NI, &data,
756 cYAML_build_error(rc, seq_no, ADD_CMD, "peer_ni", err_str, err_rc);
760 int lustre_lnet_del_peer_nid(char *pnid, char **nid, int num_nids,
761 int seq_no, struct cYAML **err_rc)
763 struct lnet_ioctl_peer_cfg data;
765 int rc = LUSTRE_CFG_RC_NO_ERR;
767 char err_str[LNET_MAX_STR_LEN] = {0};
768 lnet_nid_t *nids = allocate_create_nid_array(nid, num_nids, err_str);
771 snprintf(err_str, sizeof(err_str),
772 "\"Primary nid is not provided\"");
773 rc = LUSTRE_CFG_RC_MISSING_PARAM;
776 prim_nid = libcfs_str2nid(pnid);
777 if (prim_nid == LNET_NID_ANY) {
778 rc = LUSTRE_CFG_RC_BAD_PARAM;
779 snprintf(err_str, sizeof(err_str),
786 snprintf(err_str, sizeof(err_str), "\"Success\"");
788 LIBCFS_IOC_INIT_V2(data, prcfg_hdr);
789 if (!nids || nids[0] == LNET_NID_ANY) {
790 rc = dispatch_peer_ni_cmd(prim_nid, LNET_NID_ANY,
791 IOC_LIBCFS_DEL_PEER_NI,
792 &data, err_str, "del");
796 for (idx = 0; nids && idx < num_nids; idx++) {
797 rc = dispatch_peer_ni_cmd(prim_nid, nids[idx],
798 IOC_LIBCFS_DEL_PEER_NI, &data,
808 cYAML_build_error(rc, seq_no, DEL_CMD, "peer_ni", err_str, err_rc);
812 int lustre_lnet_config_route(char *nw, char *gw, int hops, int prio,
813 int seq_no, struct cYAML **err_rc)
815 struct lnet_ioctl_config_data data;
816 lnet_nid_t gateway_nid;
817 int rc = LUSTRE_CFG_RC_NO_ERR;
818 __u32 net = LNET_NIDNET(LNET_NID_ANY);
819 char err_str[LNET_MAX_STR_LEN];
821 snprintf(err_str, sizeof(err_str), "\"Success\"");
823 if (nw == NULL || gw == NULL) {
826 "\"missing mandatory parameter in route config:'%s'\"",
827 (nw == NULL && gw == NULL) ? "network, gateway" :
828 (nw == NULL) ? "network" : "gateway");
829 rc = LUSTRE_CFG_RC_MISSING_PARAM;
833 net = libcfs_str2net(nw);
834 if (net == LNET_NIDNET(LNET_NID_ANY)) {
837 "\"cannot parse net %s\"", nw);
838 rc = LUSTRE_CFG_RC_BAD_PARAM;
842 gateway_nid = libcfs_str2nid(gw);
843 if (gateway_nid == LNET_NID_ANY) {
846 "\"cannot parse gateway NID '%s'\"", gw);
847 rc = LUSTRE_CFG_RC_BAD_PARAM;
852 /* hops is undefined */
853 hops = LNET_UNDEFINED_HOPS;
854 } else if (hops < 1 || hops > 255) {
857 "\"invalid hop count %d, must be between 1 and 255\"",
859 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
865 } else if (prio < 0) {
868 "\"invalid priority %d, must be greater than 0\"",
870 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
874 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
876 data.cfg_config_u.cfg_route.rtr_hop = hops;
877 data.cfg_config_u.cfg_route.rtr_priority = prio;
878 data.cfg_nid = gateway_nid;
880 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
885 "\"cannot add route: %s\"", strerror(errno));
890 cYAML_build_error(rc, seq_no, ADD_CMD, "route", err_str, err_rc);
895 int lustre_lnet_del_route(char *nw, char *gw,
896 int seq_no, struct cYAML **err_rc)
898 struct lnet_ioctl_config_data data;
899 lnet_nid_t gateway_nid;
900 int rc = LUSTRE_CFG_RC_NO_ERR;
901 __u32 net = LNET_NIDNET(LNET_NID_ANY);
902 char err_str[LNET_MAX_STR_LEN];
904 snprintf(err_str, sizeof(err_str), "\"Success\"");
906 if (nw == NULL || gw == NULL) {
909 "\"missing mandatory parameter in route delete: '%s'\"",
910 (nw == NULL && gw == NULL) ? "network, gateway" :
911 (nw == NULL) ? "network" : "gateway");
912 rc = LUSTRE_CFG_RC_MISSING_PARAM;
916 net = libcfs_str2net(nw);
917 if (net == LNET_NIDNET(LNET_NID_ANY)) {
920 "\"cannot parse net '%s'\"", nw);
921 rc = LUSTRE_CFG_RC_BAD_PARAM;
925 gateway_nid = libcfs_str2nid(gw);
926 if (gateway_nid == LNET_NID_ANY) {
929 "\"cannot parse gateway NID '%s'\"", gw);
930 rc = LUSTRE_CFG_RC_BAD_PARAM;
934 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
936 data.cfg_nid = gateway_nid;
938 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
943 "\"cannot delete route: %s\"", strerror(errno));
948 cYAML_build_error(rc, seq_no, DEL_CMD, "route", err_str, err_rc);
953 int lustre_lnet_show_route(char *nw, char *gw, int hops, int prio, int detail,
954 int seq_no, struct cYAML **show_rc,
955 struct cYAML **err_rc, bool backup)
957 struct lnet_ioctl_config_data data;
958 lnet_nid_t gateway_nid;
959 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
961 __u32 net = LNET_NIDNET(LNET_NID_ANY);
963 struct cYAML *root = NULL, *route = NULL, *item = NULL;
964 struct cYAML *first_seq = NULL;
965 char err_str[LNET_MAX_STR_LEN];
968 snprintf(err_str, sizeof(err_str),
969 "\"out of memory\"");
972 net = libcfs_str2net(nw);
973 if (net == LNET_NIDNET(LNET_NID_ANY)) {
976 "\"cannot parse net '%s'\"", nw);
977 rc = LUSTRE_CFG_RC_BAD_PARAM;
982 /* show all routes without filtering on net */
983 net = LNET_NIDNET(LNET_NID_ANY);
987 gateway_nid = libcfs_str2nid(gw);
988 if (gateway_nid == LNET_NID_ANY) {
991 "\"cannot parse gateway NID '%s'\"", gw);
992 rc = LUSTRE_CFG_RC_BAD_PARAM;
996 /* show all routes with out filtering on gateway */
997 gateway_nid = LNET_NID_ANY;
999 if ((hops < 1 && hops != -1) || hops > 255) {
1002 "\"invalid hop count %d, must be between 0 and 256\"",
1004 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1008 /* create struct cYAML root object */
1009 root = cYAML_create_object(NULL, NULL);
1013 route = cYAML_create_seq(root, "route");
1018 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1021 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
1027 /* filter on provided data */
1028 if (net != LNET_NIDNET(LNET_NID_ANY) &&
1029 net != data.cfg_net)
1032 if (gateway_nid != LNET_NID_ANY &&
1033 gateway_nid != data.cfg_nid)
1037 hops != data.cfg_config_u.cfg_route.rtr_hop)
1041 prio != data.cfg_config_u.cfg_route.rtr_priority)
1044 /* default rc to -1 incase we hit the goto */
1048 item = cYAML_create_seq_item(route);
1052 if (first_seq == NULL)
1055 if (cYAML_create_string(item, "net",
1056 libcfs_net2str(data.cfg_net)) == NULL)
1059 if (cYAML_create_string(item, "gateway",
1060 libcfs_nid2str(data.cfg_nid)) == NULL)
1064 if (cYAML_create_number(item, "hop",
1065 (int) data.cfg_config_u.
1066 cfg_route.rtr_hop) ==
1070 if (cYAML_create_number(item, "priority",
1072 cfg_route.rtr_priority) == NULL)
1076 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;
1151 static int lustre_lnet_queryip(struct lnet_dlc_intf_descr *intf, __u32 *ip)
1156 memset(&ifr, 0, sizeof(ifr));
1157 rc = socket_intf_query(SIOCGIFFLAGS, intf->intf_name, &ifr);
1159 return LUSTRE_CFG_RC_BAD_PARAM;
1161 if ((ifr.ifr_flags & IFF_UP) == 0)
1162 return LUSTRE_CFG_RC_BAD_PARAM;
1164 memset(&ifr, 0, sizeof(ifr));
1165 rc = socket_intf_query(SIOCGIFADDR, intf->intf_name, &ifr);
1167 return LUSTRE_CFG_RC_BAD_PARAM;
1169 *ip = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
1170 *ip = bswap_32(*ip);
1172 return LUSTRE_CFG_RC_NO_ERR;
1176 * for each interface in the array of interfaces find the IP address of
1177 * that interface, create its nid and add it to an array of NIDs.
1178 * Stop if any of the interfaces is down
1180 static int lustre_lnet_intf2nids(struct lnet_dlc_network_descr *nw,
1181 lnet_nid_t **nids, __u32 *nnids,
1182 char *err_str, size_t str_len)
1184 int i = 0, count = 0, rc;
1185 struct lnet_dlc_intf_descr *intf;
1186 char val[LNET_MAX_STR_LEN];
1191 if (nw == NULL || nids == NULL) {
1192 snprintf(err_str, str_len,
1193 "\"unexpected parameters to lustre_lnet_intf2nids()\"");
1194 err_str[str_len - 1] = '\0';
1195 return LUSTRE_CFG_RC_BAD_PARAM;
1198 if (LNET_NETTYP(nw->nw_id) == GNILND) {
1201 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network)
1205 *nids = calloc(count, sizeof(lnet_nid_t));
1206 if (*nids == NULL) {
1207 snprintf(err_str, str_len,
1208 "\"out of memory\"");
1209 err_str[str_len - 1] = '\0';
1210 return LUSTRE_CFG_RC_OUT_OF_MEM;
1213 * special case the GNI interface since it doesn't have an IP
1214 * address. The assumption is that there can only be one GNI
1215 * interface in the system. No interface name is provided.
1217 if (LNET_NETTYP(nw->nw_id) == GNILND) {
1218 rc = read_sysfs_file(gni_nid_path, "nid", val,
1221 snprintf(err_str, str_len,
1222 "\"cannot read gni nid\"");
1223 err_str[str_len - 1] = '\0';
1226 gni_num = atoi(val);
1228 (*nids)[i] = LNET_MKNID(nw->nw_id, gni_num);
1233 /* look at the other interfaces */
1234 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network) {
1235 rc = lustre_lnet_queryip(intf, &ip);
1236 if (rc != LUSTRE_CFG_RC_NO_ERR) {
1237 snprintf(err_str, str_len,
1238 "\"couldn't query intf %s\"", intf->intf_name);
1239 err_str[str_len - 1] = '\0';
1242 (*nids)[i] = LNET_MKNID(nw->nw_id, ip);
1258 * called repeatedly until a match or no more ip range
1260 * ip_range expression
1261 * interface list with all the interface names.
1262 * all the interfaces in the system.
1264 * try to match the ip_range expr to one of the interfaces' IPs in
1265 * the system. If we hit a patch for an interface. Check if that
1266 * interface name is in the list.
1268 * If there are more than one interface in the list, then make sure
1269 * that the IPs for all of these interfaces match the ip ranges
1272 * for each interface in intf_list
1273 * look up the intf name in ifa
1274 * if not there then no match
1275 * check ip obtained from ifa against a match to any of the
1277 * If no match, then fail
1279 * The result is that all the interfaces have to match.
1281 int lustre_lnet_match_ip_to_intf(struct ifaddrs *ifa,
1282 struct list_head *intf_list,
1283 struct list_head *ip_ranges)
1287 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1288 struct ifaddrs *ifaddr = ifa;
1289 struct lustre_lnet_ip_range_descr *ip_range;
1293 * if there are no explicit interfaces, and no ip ranges, then
1294 * configure the first tcp interface we encounter.
1296 if (list_empty(intf_list) && list_empty(ip_ranges)) {
1297 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1298 if (ifaddr->ifa_addr == NULL)
1301 if ((ifaddr->ifa_flags & IFF_UP) == 0)
1304 family = ifaddr->ifa_addr->sa_family;
1305 if (family == AF_INET &&
1306 strcmp(ifaddr->ifa_name, "lo") != 0) {
1307 rc = lustre_lnet_add_intf_descr
1308 (intf_list, ifaddr->ifa_name,
1309 strlen(ifaddr->ifa_name));
1311 if (rc != LUSTRE_CFG_RC_NO_ERR)
1314 return LUSTRE_CFG_RC_MATCH;
1317 return LUSTRE_CFG_RC_NO_MATCH;
1321 * First interface which matches an IP pattern will be used
1323 if (list_empty(intf_list)) {
1325 * no interfaces provided in the rule, but an ip range is
1326 * provided, so try and match an interface to the ip
1329 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1330 if (ifaddr->ifa_addr == NULL)
1333 if ((ifaddr->ifa_flags & IFF_UP) == 0)
1336 family = ifaddr->ifa_addr->sa_family;
1337 if (family == AF_INET) {
1338 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->
1341 list_for_each_entry(ip_range, ip_ranges,
1343 rc = cfs_ip_addr_match(bswap_32(ip),
1344 &ip_range->ipr_expr);
1348 rc = lustre_lnet_add_intf_descr
1349 (intf_list, ifaddr->ifa_name,
1350 strlen(ifaddr->ifa_name));
1352 if (rc != LUSTRE_CFG_RC_NO_ERR)
1358 if (!list_empty(intf_list))
1359 return LUSTRE_CFG_RC_MATCH;
1361 return LUSTRE_CFG_RC_NO_MATCH;
1365 * If an interface is explicitly specified the ip-range might or
1366 * might not be specified. if specified the interface needs to match the
1367 * ip-range. If no ip-range then the interfaces are
1368 * automatically matched if they are all up.
1369 * If > 1 interfaces all the interfaces must match for the NI to
1372 list_for_each_entry_safe(intf_descr, tmp, intf_list, intf_on_network) {
1373 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1374 if (ifaddr->ifa_addr == NULL)
1377 family = ifaddr->ifa_addr->sa_family;
1378 if (family == AF_INET &&
1379 strcmp(intf_descr->intf_name,
1380 ifaddr->ifa_name) == 0)
1384 if (ifaddr == NULL) {
1385 list_del(&intf_descr->intf_on_network);
1386 free_intf_descr(intf_descr);
1390 if ((ifaddr->ifa_flags & IFF_UP) == 0) {
1391 list_del(&intf_descr->intf_on_network);
1392 free_intf_descr(intf_descr);
1396 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr.s_addr;
1399 list_for_each_entry(ip_range, ip_ranges, ipr_entry) {
1400 rc = cfs_ip_addr_match(bswap_32(ip), &ip_range->ipr_expr);
1406 /* no match for this interface */
1407 list_del(&intf_descr->intf_on_network);
1408 free_intf_descr(intf_descr);
1412 return LUSTRE_CFG_RC_MATCH;
1415 static int lustre_lnet_resolve_ip2nets_rule(struct lustre_lnet_ip2nets *ip2nets,
1416 lnet_nid_t **nids, __u32 *nnids,
1417 char *err_str, size_t str_len)
1419 struct ifaddrs *ifa;
1420 int rc = LUSTRE_CFG_RC_NO_ERR;
1422 rc = getifaddrs(&ifa);
1424 snprintf(err_str, str_len,
1425 "\"failed to get interface addresses: %d\"", -errno);
1426 err_str[str_len - 1] = '\0';
1430 rc = lustre_lnet_match_ip_to_intf(ifa,
1431 &ip2nets->ip2nets_net.nw_intflist,
1432 &ip2nets->ip2nets_ip_ranges);
1433 if (rc != LUSTRE_CFG_RC_MATCH) {
1434 snprintf(err_str, str_len,
1435 "\"couldn't match ip to existing interfaces\"");
1436 err_str[str_len - 1] = '\0';
1441 rc = lustre_lnet_intf2nids(&ip2nets->ip2nets_net, nids, nnids,
1442 err_str, sizeof(err_str));
1443 if (rc != LUSTRE_CFG_RC_NO_ERR) {
1454 lustre_lnet_ioctl_config_ni(struct list_head *intf_list,
1455 struct lnet_ioctl_config_lnd_tunables *tunables,
1456 struct cfs_expr_list *global_cpts,
1457 lnet_nid_t *nids, char *err_str)
1460 struct lnet_ioctl_config_ni *conf;
1461 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1462 int rc = LUSTRE_CFG_RC_NO_ERR, i = 0;
1465 struct lnet_dlc_intf_descr *intf_descr;
1467 struct cfs_expr_list *cpt_expr;
1469 list_for_each_entry(intf_descr, intf_list,
1471 if (tunables != NULL)
1472 len = sizeof(struct lnet_ioctl_config_ni) +
1473 sizeof(struct lnet_ioctl_config_lnd_tunables);
1475 len = sizeof(struct lnet_ioctl_config_ni);
1477 data = calloc(1, len);
1479 return LUSTRE_CFG_RC_OUT_OF_MEM;
1480 conf = (struct lnet_ioctl_config_ni*) data;
1481 if (tunables != NULL)
1482 tun = (struct lnet_ioctl_config_lnd_tunables*)
1485 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1486 conf->lic_cfg_hdr.ioc_len = len;
1487 conf->lic_nid = nids[i];
1488 strncpy(conf->lic_ni_intf[0], intf_descr->intf_name,
1491 if (intf_descr->cpt_expr != NULL)
1492 cpt_expr = intf_descr->cpt_expr;
1493 else if (global_cpts != NULL)
1494 cpt_expr = global_cpts;
1498 if (cpt_expr != NULL) {
1499 count = cfs_expr_list_values(cpt_expr,
1500 LNET_MAX_SHOW_NUM_CPT,
1503 memcpy(conf->lic_cpts, cpt_array,
1504 sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1513 conf->lic_ncpts = count;
1515 if (tunables != NULL)
1516 memcpy(tun, tunables, sizeof(*tunables));
1518 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1523 "\"cannot add network: %s\"", strerror(errno));
1531 return LUSTRE_CFG_RC_NO_ERR;
1535 lustre_lnet_config_ip2nets(struct lustre_lnet_ip2nets *ip2nets,
1536 struct lnet_ioctl_config_lnd_tunables *tunables,
1537 struct cfs_expr_list *global_cpts,
1538 int seq_no, struct cYAML **err_rc)
1540 lnet_nid_t *nids = NULL;
1543 char err_str[LNET_MAX_STR_LEN];
1545 snprintf(err_str, sizeof(err_str), "\"success\"");
1550 "\"incomplete ip2nets information\"");
1551 rc = LUSTRE_CFG_RC_BAD_PARAM;
1556 * call below function to resolve the rules into a list of nids.
1557 * The memory is allocated in that function then freed here when
1558 * it's no longer needed.
1560 rc = lustre_lnet_resolve_ip2nets_rule(ip2nets, &nids, &nnids, err_str,
1562 if (rc != LUSTRE_CFG_RC_NO_ERR && rc != LUSTRE_CFG_RC_MATCH)
1565 if (list_empty(&ip2nets->ip2nets_net.nw_intflist)) {
1566 snprintf(err_str, sizeof(err_str),
1567 "\"no interfaces match ip2nets rules\"");
1571 rc = lustre_lnet_ioctl_config_ni(&ip2nets->ip2nets_net.nw_intflist,
1572 tunables, global_cpts, nids,
1579 cYAML_build_error(rc, seq_no, ADD_CMD, "ip2nets", err_str, err_rc);
1583 int lustre_lnet_config_ni(struct lnet_dlc_network_descr *nw_descr,
1584 struct cfs_expr_list *global_cpts,
1586 struct lnet_ioctl_config_lnd_tunables *tunables,
1587 int seq_no, struct cYAML **err_rc)
1590 struct lnet_ioctl_config_ni *conf;
1591 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1592 char buf[LNET_MAX_STR_LEN];
1593 int rc = LUSTRE_CFG_RC_NO_ERR;
1594 char err_str[LNET_MAX_STR_LEN];
1595 lnet_nid_t *nids = NULL;
1599 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1602 snprintf(err_str, sizeof(err_str), "\"success\"");
1604 if (ip2net == NULL && (nw_descr == NULL || nw_descr->nw_id == 0 ||
1605 (list_empty(&nw_descr->nw_intflist) &&
1606 LNET_NETTYP(nw_descr->nw_id) != GNILND))) {
1609 "\"missing mandatory parameters in NI config: '%s'\"",
1610 (nw_descr == NULL) ? "network , interface" :
1611 (nw_descr->nw_id == 0) ? "network" : "interface");
1612 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1616 if (ip2net != NULL && strlen(ip2net) >= sizeof(buf)) {
1619 "\"ip2net string too long %d\"",
1620 (int)strlen(ip2net));
1621 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1625 if (ip2net != NULL) {
1626 if (tunables != NULL)
1627 len = sizeof(struct lnet_ioctl_config_ni) +
1628 sizeof(struct lnet_ioctl_config_lnd_tunables);
1630 len = sizeof(struct lnet_ioctl_config_ni);
1631 data = calloc(1, len);
1633 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1636 conf = (struct lnet_ioctl_config_ni*) data;
1637 if (tunables != NULL)
1638 tun = (struct lnet_ioctl_config_lnd_tunables*)
1639 (data + sizeof(*conf));
1641 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1642 conf->lic_cfg_hdr.ioc_len = len;
1643 strncpy(conf->lic_legacy_ip2nets, ip2net,
1646 if (global_cpts != NULL) {
1647 count = cfs_expr_list_values(global_cpts,
1648 LNET_MAX_SHOW_NUM_CPT,
1651 memcpy(conf->lic_cpts, cpt_array,
1652 sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1661 conf->lic_ncpts = count;
1663 if (tunables != NULL)
1664 memcpy(tun, tunables, sizeof(*tunables));
1666 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1671 "\"cannot add network: %s\"", strerror(errno));
1678 if (LNET_NETTYP(nw_descr->nw_id) == LOLND) {
1679 rc = LUSTRE_CFG_RC_NO_ERR;
1683 if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1686 "\"cannot parse net '%s'\"",
1687 libcfs_net2str(nw_descr->nw_id));
1688 rc = LUSTRE_CFG_RC_BAD_PARAM;
1693 * special case the GNI since no interface name is expected
1695 if (list_empty(&nw_descr->nw_intflist) &&
1696 (LNET_NETTYP(nw_descr->nw_id) != GNILND)) {
1699 "\"no interface name provided\"");
1700 rc = LUSTRE_CFG_RC_BAD_PARAM;
1704 rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids,
1705 err_str, sizeof(err_str));
1707 rc = LUSTRE_CFG_RC_BAD_PARAM;
1711 rc = lustre_lnet_ioctl_config_ni(&nw_descr->nw_intflist,
1712 tunables, global_cpts, nids,
1716 if (nw_descr != NULL) {
1717 list_for_each_entry_safe(intf_descr, tmp,
1718 &nw_descr->nw_intflist,
1720 list_del(&intf_descr->intf_on_network);
1721 free_intf_descr(intf_descr);
1725 cYAML_build_error(rc, seq_no, ADD_CMD, "net", err_str, err_rc);
1736 int lustre_lnet_del_ni(struct lnet_dlc_network_descr *nw_descr,
1737 int seq_no, struct cYAML **err_rc)
1739 struct lnet_ioctl_config_ni data;
1740 int rc = LUSTRE_CFG_RC_NO_ERR, i;
1741 char err_str[LNET_MAX_STR_LEN];
1742 lnet_nid_t *nids = NULL;
1744 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1746 snprintf(err_str, sizeof(err_str), "\"success\"");
1748 if (nw_descr == NULL || nw_descr->nw_id == 0) {
1751 "\"missing mandatory parameter in deleting NI: '%s'\"",
1752 (nw_descr == NULL) ? "network , interface" :
1753 (nw_descr->nw_id == 0) ? "network" : "interface");
1754 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1758 if (LNET_NETTYP(nw_descr->nw_id) == LOLND)
1759 return LUSTRE_CFG_RC_NO_ERR;
1761 if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1764 "\"cannot parse net '%s'\"",
1765 libcfs_net2str(nw_descr->nw_id));
1766 rc = LUSTRE_CFG_RC_BAD_PARAM;
1770 rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids,
1771 err_str, sizeof(err_str));
1773 rc = LUSTRE_CFG_RC_BAD_PARAM;
1778 * no interfaces just the nw_id is specified
1781 nids = calloc(1, sizeof(*nids));
1783 snprintf(err_str, sizeof(err_str),
1784 "\"out of memory\"");
1785 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1788 nids[0] = LNET_MKNID(nw_descr->nw_id, 0);
1792 for (i = 0; i < nnids; i++) {
1793 LIBCFS_IOC_INIT_V2(data, lic_cfg_hdr);
1794 data.lic_nid = nids[i];
1796 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_LOCAL_NI, &data);
1801 "\"cannot del network: %s\"", strerror(errno));
1805 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
1807 list_del(&intf_descr->intf_on_network);
1808 free_intf_descr(intf_descr);
1812 cYAML_build_error(rc, seq_no, DEL_CMD, "net", err_str, err_rc);
1821 add_msg_stats_to_yaml_blk(struct cYAML *yaml,
1822 struct lnet_ioctl_comm_count *counts)
1824 if (cYAML_create_number(yaml, "put",
1825 counts->ico_put_count)
1828 if (cYAML_create_number(yaml, "get",
1829 counts->ico_get_count)
1832 if (cYAML_create_number(yaml, "reply",
1833 counts->ico_reply_count)
1836 if (cYAML_create_number(yaml, "ack",
1837 counts->ico_ack_count)
1840 if (cYAML_create_number(yaml, "hello",
1841 counts->ico_hello_count)
1848 static struct lnet_ioctl_comm_count *
1849 get_counts(struct lnet_ioctl_element_msg_stats *msg_stats, int idx)
1852 return &msg_stats->im_send_stats;
1854 return &msg_stats->im_recv_stats;
1856 return &msg_stats->im_drop_stats;
1861 int lustre_lnet_show_net(char *nw, int detail, int seq_no,
1862 struct cYAML **show_rc, struct cYAML **err_rc,
1866 struct lnet_ioctl_config_ni *ni_data;
1867 struct lnet_ioctl_config_lnd_tunables *lnd;
1868 struct lnet_ioctl_element_stats *stats;
1869 struct lnet_ioctl_element_msg_stats msg_stats;
1870 __u32 net = LNET_NIDNET(LNET_NID_ANY);
1871 __u32 prev_net = LNET_NIDNET(LNET_NID_ANY);
1872 int rc = LUSTRE_CFG_RC_OUT_OF_MEM, i, j;
1874 struct cYAML *root = NULL, *tunables = NULL,
1875 *net_node = NULL, *interfaces = NULL,
1876 *item = NULL, *first_seq = NULL,
1877 *tmp = NULL, *statistics = NULL;
1878 int str_buf_len = LNET_MAX_SHOW_NUM_CPT * 2;
1879 char str_buf[str_buf_len];
1881 char err_str[LNET_MAX_STR_LEN];
1882 bool exist = false, new_net = true;
1884 size_t buf_size = sizeof(*ni_data) + sizeof(*lnd) + sizeof(*stats);
1886 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1888 buf = calloc(1, buf_size);
1892 ni_data = (struct lnet_ioctl_config_ni *)buf;
1895 net = libcfs_str2net(nw);
1896 if (net == LNET_NIDNET(LNET_NID_ANY)) {
1899 "\"cannot parse net '%s'\"", nw);
1900 rc = LUSTRE_CFG_RC_BAD_PARAM;
1905 root = cYAML_create_object(NULL, NULL);
1909 net_node = cYAML_create_seq(root, "net");
1910 if (net_node == NULL)
1917 memset(buf, 0, buf_size);
1919 LIBCFS_IOC_INIT_V2(*ni_data, lic_cfg_hdr);
1921 * set the ioc_len to the proper value since INIT assumes
1924 ni_data->lic_cfg_hdr.ioc_len = buf_size;
1925 ni_data->lic_idx = i;
1927 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LOCAL_NI, ni_data);
1933 rc_net = LNET_NIDNET(ni_data->lic_nid);
1935 /* filter on provided data */
1936 if (net != LNET_NIDNET(LNET_NID_ANY) &&
1940 /* if we're backing up don't store lo */
1941 if (backup && LNET_NETTYP(rc_net) == LOLND)
1944 /* default rc to -1 in case we hit the goto */
1948 stats = (struct lnet_ioctl_element_stats *)ni_data->lic_bulk;
1949 lnd = (struct lnet_ioctl_config_lnd_tunables *)
1950 (ni_data->lic_bulk + sizeof(*stats));
1952 if (rc_net != prev_net) {
1959 if (!cYAML_create_string(net_node, "net type",
1960 libcfs_net2str(rc_net)))
1963 tmp = cYAML_create_seq(net_node, "local NI(s)");
1969 /* create the tree to be printed. */
1970 item = cYAML_create_seq_item(tmp);
1974 if (first_seq == NULL)
1978 cYAML_create_string(item, "nid",
1979 libcfs_nid2str(ni_data->lic_nid)) == NULL)
1983 cYAML_create_string(item,
1985 (ni_data->lic_status ==
1986 LNET_NI_STATUS_UP) ?
1987 "up" : "down") == NULL)
1990 /* don't add interfaces unless there is at least one
1992 if (strlen(ni_data->lic_ni_intf[0]) > 0) {
1993 interfaces = cYAML_create_object(item, "interfaces");
1994 if (interfaces == NULL)
1997 for (j = 0; j < LNET_INTERFACES_NUM; j++) {
1998 if (strlen(ni_data->lic_ni_intf[j]) > 0) {
2000 sizeof(str_buf), "%d", j);
2001 if (cYAML_create_string(interfaces,
2003 ni_data->lic_ni_intf[j]) ==
2015 goto continue_without_msg_stats;
2017 statistics = cYAML_create_object(item, "statistics");
2018 if (statistics == NULL)
2021 if (cYAML_create_number(statistics, "send_count",
2022 stats->iel_send_count)
2026 if (cYAML_create_number(statistics, "recv_count",
2027 stats->iel_recv_count)
2031 if (cYAML_create_number(statistics, "drop_count",
2032 stats->iel_drop_count)
2037 goto continue_without_msg_stats;
2039 LIBCFS_IOC_INIT_V2(msg_stats, im_hdr);
2040 msg_stats.im_hdr.ioc_len = sizeof(msg_stats);
2041 msg_stats.im_idx = i;
2043 rc = l_ioctl(LNET_DEV_ID,
2044 IOC_LIBCFS_GET_LOCAL_NI_MSG_STATS,
2048 goto continue_without_msg_stats;
2051 for (k = 0; k < 3; k++) {
2052 struct lnet_ioctl_comm_count *counts;
2053 struct cYAML *msg_statistics = NULL;
2055 msg_statistics = cYAML_create_object(item,
2056 (char *)gmsg_stat_names[k]);
2057 if (msg_statistics == NULL)
2060 counts = get_counts(&msg_stats, k);
2064 if (!add_msg_stats_to_yaml_blk(msg_statistics,
2069 continue_without_msg_stats:
2070 tunables = cYAML_create_object(item, "tunables");
2074 rc = lustre_net_show_tunables(tunables, &lnd->lt_cmn);
2075 if (rc != LUSTRE_CFG_RC_NO_ERR)
2078 rc = lustre_ni_show_tunables(tunables, LNET_NETTYP(rc_net),
2080 if (rc != LUSTRE_CFG_RC_NO_ERR &&
2081 rc != LUSTRE_CFG_RC_NO_MATCH)
2084 if (rc != LUSTRE_CFG_RC_NO_MATCH) {
2085 tunables = cYAML_create_object(item,
2087 if (tunables == NULL)
2092 cYAML_create_number(item, "dev cpt",
2093 ni_data->lic_dev_cpt) == NULL)
2097 cYAML_create_number(item, "tcp bonding",
2098 ni_data->lic_tcp_bonding)
2102 /* out put the CPTs in the format: "[x,x,x,...]" */
2103 limit = str_buf + str_buf_len - 3;
2104 pos += snprintf(pos, limit - pos, "\"[");
2105 for (j = 0 ; ni_data->lic_ncpts >= 1 &&
2106 j < ni_data->lic_ncpts &&
2108 pos += snprintf(pos, limit - pos,
2109 "%d", ni_data->lic_cpts[j]);
2110 if ((j + 1) < ni_data->lic_ncpts)
2111 pos += snprintf(pos, limit - pos, ",");
2113 pos += snprintf(pos, 3, "]\"");
2115 if (ni_data->lic_ncpts >= 1 &&
2116 cYAML_create_string(item, "CPT",
2122 /* Print out the net information only if show_rc is not provided */
2123 if (show_rc == NULL)
2124 cYAML_print_tree(root);
2126 if (l_errno != ENOENT) {
2129 "\"cannot get networks: %s\"",
2134 rc = LUSTRE_CFG_RC_NO_ERR;
2136 snprintf(err_str, sizeof(err_str), "\"success\"");
2138 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2139 cYAML_free_tree(root);
2140 } else if (show_rc != NULL && *show_rc != NULL) {
2141 struct cYAML *show_node;
2142 /* find the net node, if one doesn't exist
2143 * then insert one. Otherwise add to the one there
2145 show_node = cYAML_get_object_item(*show_rc, "net");
2146 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2147 cYAML_insert_child(show_node, first_seq);
2150 } else if (show_node == NULL) {
2151 cYAML_insert_sibling((*show_rc)->cy_child,
2155 cYAML_free_tree(root);
2161 cYAML_build_error(rc, seq_no, SHOW_CMD, "net", err_str, err_rc);
2166 int lustre_lnet_enable_routing(int enable, int seq_no, struct cYAML **err_rc)
2168 struct lnet_ioctl_config_data data;
2169 int rc = LUSTRE_CFG_RC_NO_ERR;
2170 char err_str[LNET_MAX_STR_LEN];
2172 snprintf(err_str, sizeof(err_str), "\"success\"");
2174 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2175 data.cfg_config_u.cfg_buffers.buf_enable = (enable) ? 1 : 0;
2177 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIG_RTR, &data);
2182 "\"cannot %s routing %s\"",
2183 (enable) ? "enable" : "disable", strerror(errno));
2188 cYAML_build_error(rc, seq_no,
2189 (enable) ? ADD_CMD : DEL_CMD,
2190 "routing", err_str, err_rc);
2195 int ioctl_set_value(__u32 val, int ioc, char *name,
2196 int seq_no, struct cYAML **err_rc)
2198 struct lnet_ioctl_set_value data;
2199 int rc = LUSTRE_CFG_RC_NO_ERR;
2200 char err_str[LNET_MAX_STR_LEN];
2202 snprintf(err_str, sizeof(err_str), "\"success\"");
2204 LIBCFS_IOC_INIT_V2(data, sv_hdr);
2205 data.sv_value = val;
2207 rc = l_ioctl(LNET_DEV_ID, ioc , &data);
2212 "\"cannot configure %s to %d: %s\"", name,
2213 val, strerror(errno));
2216 cYAML_build_error(rc, seq_no, ADD_CMD, name, err_str, err_rc);
2221 int lustre_lnet_config_max_intf(int max, int seq_no, struct cYAML **err_rc)
2223 int rc = LUSTRE_CFG_RC_NO_ERR;
2224 char err_str[LNET_MAX_STR_LEN];
2225 char val[LNET_MAX_STR_LEN];
2227 snprintf(err_str, sizeof(err_str), "\"success\"");
2229 snprintf(val, sizeof(val), "%d", max);
2231 rc = write_sysfs_file(modparam_path, "lnet_interfaces_max", val,
2232 1, strlen(val) + 1);
2234 snprintf(err_str, sizeof(err_str),
2235 "\"cannot configure max interfaces: %s\"",
2238 cYAML_build_error(rc, seq_no, ADD_CMD, "max_interfaces", err_str, err_rc);
2243 int lustre_lnet_config_discovery(int enable, int seq_no, struct cYAML **err_rc)
2245 int rc = LUSTRE_CFG_RC_NO_ERR;
2246 char err_str[LNET_MAX_STR_LEN];
2247 char val[LNET_MAX_STR_LEN];
2249 snprintf(err_str, sizeof(err_str), "\"success\"");
2251 snprintf(val, sizeof(val), "%u", (enable) ? 0 : 1);
2253 rc = write_sysfs_file(modparam_path, "lnet_peer_discovery_disabled", val,
2254 1, strlen(val) + 1);
2256 snprintf(err_str, sizeof(err_str),
2257 "\"cannot configure discovery: %s\"",
2260 cYAML_build_error(rc, seq_no, ADD_CMD, "discovery", err_str, err_rc);
2266 int lustre_lnet_config_numa_range(int range, int seq_no, struct cYAML **err_rc)
2268 return ioctl_set_value(range, IOC_LIBCFS_SET_NUMA_RANGE,
2269 "numa_range", seq_no, err_rc);
2272 int lustre_lnet_config_buffers(int tiny, int small, int large, int seq_no,
2273 struct cYAML **err_rc)
2275 struct lnet_ioctl_config_data data;
2276 int rc = LUSTRE_CFG_RC_NO_ERR;
2277 char err_str[LNET_MAX_STR_LEN];
2279 snprintf(err_str, sizeof(err_str), "\"success\"");
2281 /* -1 indicates to ignore changes to this field */
2282 if (tiny < -1 || small < -1 || large < -1) {
2285 "\"tiny, small and large must be >= 0\"");
2286 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
2290 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2291 data.cfg_config_u.cfg_buffers.buf_tiny = tiny;
2292 data.cfg_config_u.cfg_buffers.buf_small = small;
2293 data.cfg_config_u.cfg_buffers.buf_large = large;
2295 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_BUF, &data);
2300 "\"cannot configure buffers: %s\"", strerror(errno));
2305 cYAML_build_error(rc, seq_no, ADD_CMD, "buf", err_str, err_rc);
2310 int lustre_lnet_show_routing(int seq_no, struct cYAML **show_rc,
2311 struct cYAML **err_rc, bool backup)
2313 struct lnet_ioctl_config_data *data;
2314 struct lnet_ioctl_pool_cfg *pool_cfg = NULL;
2315 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2318 char *pools[LNET_NRBPOOLS] = {"tiny", "small", "large"};
2319 int buf_count[LNET_NRBPOOLS] = {0};
2320 struct cYAML *root = NULL, *pools_node = NULL,
2321 *type_node = NULL, *item = NULL, *cpt = NULL,
2322 *first_seq = NULL, *buffers = NULL;
2324 char err_str[LNET_MAX_STR_LEN];
2325 char node_name[LNET_MAX_STR_LEN];
2328 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2330 buf = calloc(1, sizeof(*data) + sizeof(*pool_cfg));
2334 data = (struct lnet_ioctl_config_data *)buf;
2336 root = cYAML_create_object(NULL, NULL);
2341 pools_node = cYAML_create_object(root, "routing");
2343 pools_node = cYAML_create_seq(root, "routing");
2344 if (pools_node == NULL)
2348 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
2349 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
2350 sizeof(struct lnet_ioctl_pool_cfg);
2351 data->cfg_count = i;
2353 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_BUF, data);
2361 pool_cfg = (struct lnet_ioctl_pool_cfg *)data->cfg_bulk;
2364 goto calculate_buffers;
2366 snprintf(node_name, sizeof(node_name), "cpt[%d]", i);
2367 item = cYAML_create_seq_item(pools_node);
2371 if (first_seq == NULL)
2374 cpt = cYAML_create_object(item, node_name);
2379 /* create the tree and print */
2380 for (j = 0; j < LNET_NRBPOOLS; j++) {
2382 type_node = cYAML_create_object(cpt, pools[j]);
2383 if (type_node == NULL)
2387 cYAML_create_number(type_node, "npages",
2388 pool_cfg->pl_pools[j].pl_npages)
2392 cYAML_create_number(type_node, "nbuffers",
2393 pool_cfg->pl_pools[j].
2394 pl_nbuffers) == NULL)
2397 cYAML_create_number(type_node, "credits",
2398 pool_cfg->pl_pools[j].
2399 pl_credits) == NULL)
2402 cYAML_create_number(type_node, "mincredits",
2403 pool_cfg->pl_pools[j].
2404 pl_mincredits) == NULL)
2406 /* keep track of the total count for each of the
2407 * tiny, small and large buffers */
2408 buf_count[j] += pool_cfg->pl_pools[j].pl_nbuffers;
2412 if (pool_cfg != NULL) {
2414 if (cYAML_create_number(pools_node, "enable",
2415 pool_cfg->pl_routing) ==
2419 goto add_buffer_section;
2422 item = cYAML_create_seq_item(pools_node);
2426 if (cYAML_create_number(item, "enable", pool_cfg->pl_routing) ==
2432 /* create a buffers entry in the show. This is necessary so that
2433 * if the YAML output is used to configure a node, the buffer
2434 * configuration takes hold */
2435 buffers = cYAML_create_object(root, "buffers");
2436 if (buffers == NULL)
2439 for (i = 0; i < LNET_NRBPOOLS; i++) {
2440 if (cYAML_create_number(buffers, pools[i], buf_count[i]) == NULL)
2444 if (show_rc == NULL)
2445 cYAML_print_tree(root);
2447 if (l_errno != ENOENT) {
2450 "\"cannot get routing information: %s\"",
2455 rc = LUSTRE_CFG_RC_NO_ERR;
2457 snprintf(err_str, sizeof(err_str), "\"success\"");
2458 rc = LUSTRE_CFG_RC_NO_ERR;
2462 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2463 cYAML_free_tree(root);
2464 } else if (show_rc != NULL && *show_rc != NULL) {
2465 struct cYAML *routing_node;
2466 /* there should exist only one routing block and one
2467 * buffers block. If there already exists a previous one
2468 * then don't add another */
2469 routing_node = cYAML_get_object_item(*show_rc, "routing");
2470 if (routing_node == NULL) {
2471 cYAML_insert_sibling((*show_rc)->cy_child,
2475 cYAML_free_tree(root);
2481 cYAML_build_error(rc, seq_no, SHOW_CMD, "routing", err_str, err_rc);
2486 int lustre_lnet_show_peer(char *knid, int detail, int seq_no,
2487 struct cYAML **show_rc, struct cYAML **err_rc,
2491 * TODO: This function is changing in a future patch to accommodate
2492 * PEER_LIST and proper filtering on any nid of the peer
2494 struct lnet_ioctl_peer_cfg peer_info;
2495 struct lnet_peer_ni_credit_info *lpni_cri;
2496 struct lnet_ioctl_element_stats *lpni_stats;
2497 struct lnet_ioctl_element_msg_stats *msg_stats;
2499 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2504 struct cYAML *root = NULL, *peer = NULL, *peer_ni = NULL,
2505 *first_seq = NULL, *peer_root = NULL, *tmp = NULL,
2506 *msg_statistics = NULL, *statistics = NULL;
2507 char err_str[LNET_MAX_STR_LEN];
2508 struct lnet_process_id *list = NULL;
2512 snprintf(err_str, sizeof(err_str),
2513 "\"out of memory\"");
2515 /* create struct cYAML root object */
2516 root = cYAML_create_object(NULL, NULL);
2520 peer_root = cYAML_create_seq(root, "peer");
2521 if (peer_root == NULL)
2525 size = count * sizeof(struct lnet_process_id);
2526 list = malloc(size);
2532 list[0].nid = libcfs_str2nid(knid);
2536 memset(&peer_info, 0, sizeof(peer_info));
2537 LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2538 peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2539 peer_info.prcfg_size = size;
2540 peer_info.prcfg_bulk = list;
2543 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_LIST,
2545 count = peer_info.prcfg_count;
2549 if (l_errno != E2BIG) {
2552 "\"cannot get peer list: %s\"",
2558 size = peer_info.prcfg_size;
2559 list = malloc(size);
2568 data = malloc(size);
2573 for (i = 0; i < count; i++) {
2575 memset(&peer_info, 0, sizeof(peer_info));
2576 LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2577 peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2578 peer_info.prcfg_prim_nid = list[i].nid;
2579 peer_info.prcfg_size = size;
2580 peer_info.prcfg_bulk = data;
2583 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_NI,
2588 if (l_errno != E2BIG) {
2591 "\"cannot get peer information: %s\"",
2597 size = peer_info.prcfg_size;
2598 data = malloc(size);
2605 peer = cYAML_create_seq_item(peer_root);
2609 if (first_seq == NULL)
2612 lnet_nid_t pnid = peer_info.prcfg_prim_nid;
2613 if (cYAML_create_string(peer, "primary nid",
2614 libcfs_nid2str(pnid))
2617 if (cYAML_create_string(peer, "Multi-Rail",
2618 peer_info.prcfg_mr ? "True" : "False")
2622 * print out the state of the peer only if details are
2627 cYAML_create_number(peer, "peer state",
2628 peer_info.prcfg_state)
2633 tmp = cYAML_create_seq(peer, "peer ni");
2638 for (j = 0; j < peer_info.prcfg_count; j++) {
2640 lpni_cri = (void*)nidp + sizeof(nidp);
2641 lpni_stats = (void *)lpni_cri + sizeof(*lpni_cri);
2642 msg_stats = (void *)lpni_stats + sizeof(*lpni_stats);
2643 lpni_data = (void *)msg_stats + sizeof(*msg_stats);
2645 peer_ni = cYAML_create_seq_item(tmp);
2646 if (peer_ni == NULL)
2649 if (cYAML_create_string(peer_ni, "nid",
2650 libcfs_nid2str(*nidp))
2657 if (cYAML_create_string(peer_ni, "state",
2658 lpni_cri->cr_aliveness)
2665 if (cYAML_create_number(peer_ni, "max_ni_tx_credits",
2666 lpni_cri->cr_ni_peer_tx_credits)
2670 if (cYAML_create_number(peer_ni, "available_tx_credits",
2671 lpni_cri->cr_peer_tx_credits)
2675 if (cYAML_create_number(peer_ni, "min_tx_credits",
2676 lpni_cri->cr_peer_min_tx_credits)
2680 if (cYAML_create_number(peer_ni, "tx_q_num_of_buf",
2681 lpni_cri->cr_peer_tx_qnob)
2685 if (cYAML_create_number(peer_ni, "available_rtr_credits",
2686 lpni_cri->cr_peer_rtr_credits)
2690 if (cYAML_create_number(peer_ni, "min_rtr_credits",
2691 lpni_cri->cr_peer_min_rtr_credits)
2695 if (cYAML_create_number(peer_ni, "refcount",
2696 lpni_cri->cr_refcount) == NULL)
2699 statistics = cYAML_create_object(peer_ni, "statistics");
2700 if (statistics == NULL)
2703 if (cYAML_create_number(statistics, "send_count",
2704 lpni_stats->iel_send_count)
2708 if (cYAML_create_number(statistics, "recv_count",
2709 lpni_stats->iel_recv_count)
2713 if (cYAML_create_number(statistics, "drop_count",
2714 lpni_stats->iel_drop_count)
2721 for (k = 0; k < 3; k++) {
2722 struct lnet_ioctl_comm_count *counts;
2724 msg_statistics = cYAML_create_object(peer_ni,
2725 (char *) gmsg_stat_names[k]);
2726 if (msg_statistics == NULL)
2729 counts = get_counts(msg_stats, k);
2733 if (!add_msg_stats_to_yaml_blk(msg_statistics,
2741 /* print output iff show_rc is not provided */
2742 if (show_rc == NULL)
2743 cYAML_print_tree(root);
2745 snprintf(err_str, sizeof(err_str), "\"success\"");
2746 rc = LUSTRE_CFG_RC_NO_ERR;
2751 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2752 cYAML_free_tree(root);
2753 } else if (show_rc != NULL && *show_rc != NULL) {
2754 struct cYAML *show_node;
2755 /* find the peer node, if one doesn't exist then
2756 * insert one. Otherwise add to the one there
2758 show_node = cYAML_get_object_item(*show_rc,
2760 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2761 cYAML_insert_child(show_node, first_seq);
2764 } else if (show_node == NULL) {
2765 cYAML_insert_sibling((*show_rc)->cy_child,
2769 cYAML_free_tree(root);
2775 cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
2781 int lustre_lnet_list_peer(int seq_no,
2782 struct cYAML **show_rc, struct cYAML **err_rc)
2784 struct lnet_ioctl_peer_cfg peer_info;
2785 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2790 struct cYAML *root = NULL, *list_root = NULL, *first_seq = NULL;
2791 char err_str[LNET_MAX_STR_LEN];
2792 struct lnet_process_id *list = NULL;
2794 snprintf(err_str, sizeof(err_str),
2795 "\"out of memory\"");
2797 memset(&peer_info, 0, sizeof(peer_info));
2799 /* create struct cYAML root object */
2800 root = cYAML_create_object(NULL, NULL);
2804 list_root = cYAML_create_seq(root, "peer list");
2805 if (list_root == NULL)
2809 size = count * sizeof(struct lnet_process_id);
2810 list = malloc(size);
2816 LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2817 peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2818 peer_info.prcfg_size = size;
2819 peer_info.prcfg_bulk = list;
2822 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_LIST, &peer_info);
2823 count = peer_info.prcfg_count;
2827 if (l_errno != E2BIG) {
2830 "\"cannot get peer list: %s\"",
2836 size = peer_info.prcfg_size;
2837 list = malloc(size);
2844 /* count is now the actual number of ids in the list. */
2845 for (i = 0; i < count; i++) {
2846 if (cYAML_create_string(list_root, "nid",
2847 libcfs_nid2str(list[i].nid))
2852 /* print output iff show_rc is not provided */
2853 if (show_rc == NULL)
2854 cYAML_print_tree(root);
2856 snprintf(err_str, sizeof(err_str), "\"success\"");
2857 rc = LUSTRE_CFG_RC_NO_ERR;
2862 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2863 cYAML_free_tree(root);
2864 } else if (show_rc != NULL && *show_rc != NULL) {
2865 struct cYAML *show_node;
2866 /* find the peer node, if one doesn't exist then
2867 * insert one. Otherwise add to the one there
2869 show_node = cYAML_get_object_item(*show_rc,
2871 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2872 cYAML_insert_child(show_node, first_seq);
2875 } else if (show_node == NULL) {
2876 cYAML_insert_sibling((*show_rc)->cy_child,
2880 cYAML_free_tree(root);
2886 cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
2892 static void add_to_global(struct cYAML *show_rc, struct cYAML *node,
2895 struct cYAML *show_node;
2897 show_node = cYAML_get_object_item(show_rc, "global");
2898 if (show_node != NULL)
2899 cYAML_insert_sibling(show_node->cy_child,
2902 cYAML_insert_sibling(show_rc->cy_child,
2907 static int build_global_yaml_entry(char *err_str, int err_len, int seq_no,
2908 char *name, __u32 value,
2909 struct cYAML **show_rc,
2910 struct cYAML **err_rc, int err)
2912 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2913 struct cYAML *root = NULL, *global = NULL;
2920 root = cYAML_create_object(NULL, NULL);
2924 global = cYAML_create_object(root, "global");
2928 if (cYAML_create_number(global, name,
2932 if (show_rc == NULL)
2933 cYAML_print_tree(root);
2935 snprintf(err_str, err_len, "\"success\"");
2937 rc = LUSTRE_CFG_RC_NO_ERR;
2940 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2941 cYAML_free_tree(root);
2942 } else if (show_rc != NULL && *show_rc != NULL) {
2943 add_to_global(*show_rc, global, root);
2948 cYAML_build_error(rc, seq_no, SHOW_CMD, "global", err_str, err_rc);
2953 static int ioctl_show_global_values(int ioc, int seq_no, char *name,
2954 struct cYAML **show_rc,
2955 struct cYAML **err_rc)
2957 struct lnet_ioctl_set_value data;
2960 char err_str[LNET_MAX_STR_LEN];
2962 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2964 LIBCFS_IOC_INIT_V2(data, sv_hdr);
2966 rc = l_ioctl(LNET_DEV_ID, ioc, &data);
2971 "\"cannot get %s: %s\"",
2972 name, strerror(l_errno));
2975 return build_global_yaml_entry(err_str, sizeof(err_str), seq_no, name,
2976 data.sv_value, show_rc, err_rc, l_errno);
2979 int lustre_lnet_show_max_intf(int seq_no, struct cYAML **show_rc,
2980 struct cYAML **err_rc)
2982 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2983 char val[LNET_MAX_STR_LEN];
2984 int max_intf = -1, l_errno = 0;
2985 char err_str[LNET_MAX_STR_LEN];
2987 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2989 rc = read_sysfs_file(modparam_path, "lnet_interfaces_max", val,
2993 snprintf(err_str, sizeof(err_str),
2994 "\"cannot get max interfaces: %d\"", rc);
2996 max_intf = atoi(val);
2999 return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3000 "max_intf", max_intf, show_rc,
3004 int lustre_lnet_show_discovery(int seq_no, struct cYAML **show_rc,
3005 struct cYAML **err_rc)
3007 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3008 char val[LNET_MAX_STR_LEN];
3009 int discovery = -1, l_errno = 0;
3010 char err_str[LNET_MAX_STR_LEN];
3012 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3014 rc = read_sysfs_file(modparam_path, "lnet_peer_discovery_disabled", val,
3018 snprintf(err_str, sizeof(err_str),
3019 "\"cannot get discovery setting: %d\"", rc);
3022 * The kernel stores a discovery disabled value. User space
3023 * shows whether discovery is enabled. So the value must be
3026 discovery = !atoi(val);
3029 return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3030 "discovery", discovery, show_rc,
3034 int lustre_lnet_show_numa_range(int seq_no, struct cYAML **show_rc,
3035 struct cYAML **err_rc)
3037 return ioctl_show_global_values(IOC_LIBCFS_GET_NUMA_RANGE, seq_no,
3038 "numa_range", show_rc, err_rc);
3041 int lustre_lnet_show_stats(int seq_no, struct cYAML **show_rc,
3042 struct cYAML **err_rc)
3044 struct lnet_ioctl_lnet_stats data;
3047 char err_str[LNET_MAX_STR_LEN];
3048 struct cYAML *root = NULL, *stats = NULL;
3050 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3052 LIBCFS_IOC_INIT_V2(data, st_hdr);
3054 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LNET_STATS, &data);
3059 "\"cannot get lnet statistics: %s\"",
3065 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3067 root = cYAML_create_object(NULL, NULL);
3071 stats = cYAML_create_object(root, "statistics");
3075 if (cYAML_create_number(stats, "msgs_alloc",
3076 data.st_cntrs.msgs_alloc) == NULL)
3079 if (cYAML_create_number(stats, "msgs_max",
3080 data.st_cntrs.msgs_max) == NULL)
3083 if (cYAML_create_number(stats, "errors",
3084 data.st_cntrs.errors) == NULL)
3087 if (cYAML_create_number(stats, "send_count",
3088 data.st_cntrs.send_count) == NULL)
3091 if (cYAML_create_number(stats, "recv_count",
3092 data.st_cntrs.recv_count) == NULL)
3095 if (cYAML_create_number(stats, "route_count",
3096 data.st_cntrs.route_count) == NULL)
3099 if (cYAML_create_number(stats, "drop_count",
3100 data.st_cntrs.drop_count) == NULL)
3103 if (cYAML_create_number(stats, "send_length",
3104 data.st_cntrs.send_length) == NULL)
3107 if (cYAML_create_number(stats, "recv_length",
3108 data.st_cntrs.recv_length) == NULL)
3111 if (cYAML_create_number(stats, "route_length",
3112 data.st_cntrs.route_length) == NULL)
3115 if (cYAML_create_number(stats, "drop_length",
3116 data.st_cntrs.drop_length) == NULL)
3119 if (show_rc == NULL)
3120 cYAML_print_tree(root);
3122 snprintf(err_str, sizeof(err_str), "\"success\"");
3123 rc = LUSTRE_CFG_RC_NO_ERR;
3125 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
3126 cYAML_free_tree(root);
3127 } else if (show_rc != NULL && *show_rc != NULL) {
3128 cYAML_insert_sibling((*show_rc)->cy_child,
3135 cYAML_build_error(rc, seq_no, SHOW_CMD, "statistics", err_str, err_rc);
3140 typedef int (*cmd_handler_t)(struct cYAML *tree,
3141 struct cYAML **show_rc,
3142 struct cYAML **err_rc);
3144 static int handle_yaml_config_route(struct cYAML *tree, struct cYAML **show_rc,
3145 struct cYAML **err_rc)
3147 struct cYAML *net, *gw, *hop, *prio, *seq_no;
3149 net = cYAML_get_object_item(tree, "net");
3150 gw = cYAML_get_object_item(tree, "gateway");
3151 hop = cYAML_get_object_item(tree, "hop");
3152 prio = cYAML_get_object_item(tree, "priority");
3153 seq_no = cYAML_get_object_item(tree, "seq_no");
3155 return lustre_lnet_config_route((net) ? net->cy_valuestring : NULL,
3156 (gw) ? gw->cy_valuestring : NULL,
3157 (hop) ? hop->cy_valueint : -1,
3158 (prio) ? prio->cy_valueint : -1,
3159 (seq_no) ? seq_no->cy_valueint : -1,
3163 static void yaml_free_string_array(char **array, int num)
3166 char **sub_array = array;
3168 for (i = 0; i < num; i++) {
3169 if (*sub_array != NULL)
3179 * 0: <intf_name>['['<expr>']']
3180 * 1: <intf_name>['['<expr>']']
3182 static int yaml_copy_intf_info(struct cYAML *intf_tree,
3183 struct lnet_dlc_network_descr *nw_descr)
3185 struct cYAML *child = NULL;
3186 int intf_num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
3187 struct lnet_dlc_intf_descr *intf_descr, *tmp;
3189 if (intf_tree == NULL || nw_descr == NULL)
3190 return LUSTRE_CFG_RC_BAD_PARAM;
3192 /* now grab all the interfaces and their cpts */
3193 child = intf_tree->cy_child;
3194 while (child != NULL) {
3195 if (child->cy_valuestring == NULL) {
3196 child = child->cy_next;
3200 if (strlen(child->cy_valuestring) >= LNET_MAX_STR_LEN)
3203 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist,
3204 child->cy_valuestring,
3205 strlen(child->cy_valuestring));
3206 if (rc != LUSTRE_CFG_RC_NO_ERR)
3210 child = child->cy_next;
3214 return LUSTRE_CFG_RC_MISSING_PARAM;
3219 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
3221 list_del(&intf_descr->intf_on_network);
3222 free_intf_descr(intf_descr);
3229 yaml_extract_cmn_tunables(struct cYAML *tree,
3230 struct lnet_ioctl_config_lnd_cmn_tunables *tunables,
3231 struct cfs_expr_list **global_cpts)
3233 struct cYAML *tun, *item, *smp;
3236 tun = cYAML_get_object_item(tree, "tunables");
3238 item = cYAML_get_object_item(tun, "peer_timeout");
3240 tunables->lct_peer_timeout = item->cy_valueint;
3241 item = cYAML_get_object_item(tun, "peer_credits");
3243 tunables->lct_peer_tx_credits = item->cy_valueint;
3244 item = cYAML_get_object_item(tun, "peer_buffer_credits");
3246 tunables->lct_peer_rtr_credits = item->cy_valueint;
3247 item = cYAML_get_object_item(tun, "credits");
3249 tunables->lct_max_tx_credits = item->cy_valueint;
3250 smp = cYAML_get_object_item(tun, "CPT");
3252 rc = cfs_expr_list_parse(smp->cy_valuestring,
3253 strlen(smp->cy_valuestring),
3254 0, UINT_MAX, global_cpts);
3256 *global_cpts = NULL;
3266 yaml_extract_tunables(struct cYAML *tree,
3267 struct lnet_ioctl_config_lnd_tunables *tunables,
3268 struct cfs_expr_list **global_cpts,
3273 rc = yaml_extract_cmn_tunables(tree, &tunables->lt_cmn,
3279 lustre_yaml_extract_lnd_tunables(tree, net_type,
3287 * - net type: <net>[<NUM>]
3289 * - nid: <ip>@<net>[<NUM>]
3292 * 0: <intf_name>['['<expr>']']
3293 * 1: <intf_name>['['<expr>']']
3295 * peer_timeout: <NUM>
3296 * peer_credits: <NUM>
3297 * peer_buffer_credits: <NUM>
3300 * peercredits_hiw: <NUM>
3301 * map_on_demand: <NUM>
3302 * concurrent_sends: <NUM>
3303 * fmr_pool_size: <NUM>
3304 * fmr_flush_trigger: <NUM>
3307 * At least one interface is required. If no interfaces are provided the
3308 * network interface can not be configured.
3310 static int handle_yaml_config_ni(struct cYAML *tree, struct cYAML **show_rc,
3311 struct cYAML **err_rc)
3313 struct cYAML *net, *intf, *seq_no, *ip2net = NULL, *local_nis = NULL,
3315 int num_entries = 0, rc;
3316 struct lnet_dlc_network_descr nw_descr;
3317 struct cfs_expr_list *global_cpts = NULL;
3318 struct lnet_ioctl_config_lnd_tunables tunables;
3321 memset(&tunables, 0, sizeof(tunables));
3323 INIT_LIST_HEAD(&nw_descr.network_on_rule);
3324 INIT_LIST_HEAD(&nw_descr.nw_intflist);
3326 ip2net = cYAML_get_object_item(tree, "ip2net");
3327 net = cYAML_get_object_item(tree, "net type");
3329 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
3331 nw_descr.nw_id = LOLND;
3334 * if neither net nor ip2nets are present, then we can not
3335 * configure the network.
3337 if (!net && !ip2net)
3338 return LUSTRE_CFG_RC_MISSING_PARAM;
3340 local_nis = cYAML_get_object_item(tree, "local NI(s)");
3341 if (local_nis == NULL)
3342 return LUSTRE_CFG_RC_MISSING_PARAM;
3344 if (!cYAML_is_sequence(local_nis))
3345 return LUSTRE_CFG_RC_BAD_PARAM;
3347 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
3348 intf = cYAML_get_object_item(item, "interfaces");
3351 num_entries = yaml_copy_intf_info(intf, &nw_descr);
3352 if (num_entries <= 0) {
3353 cYAML_build_error(num_entries, -1, "ni", "add",
3354 "bad interface list",
3356 return LUSTRE_CFG_RC_BAD_PARAM;
3360 found = yaml_extract_tunables(tree, &tunables, &global_cpts,
3361 LNET_NETTYP(nw_descr.nw_id));
3362 seq_no = cYAML_get_object_item(tree, "seq_no");
3364 rc = lustre_lnet_config_ni(&nw_descr,
3366 (ip2net) ? ip2net->cy_valuestring : NULL,
3367 (found) ? &tunables: NULL,
3368 (seq_no) ? seq_no->cy_valueint : -1,
3371 if (global_cpts != NULL)
3372 cfs_expr_list_free(global_cpts);
3379 * - net-spec: <tcp|o2ib|gni>[NUM]
3381 * 0: <intf name>['['<expr>']']
3382 * 1: <intf name>['['<expr>']']
3384 * 0: <expr.expr.expr.expr>
3385 * 1: <expr.expr.expr.expr>
3387 static int handle_yaml_config_ip2nets(struct cYAML *tree,
3388 struct cYAML **show_rc,
3389 struct cYAML **err_rc)
3391 struct cYAML *net, *ip_range, *item = NULL, *intf = NULL,
3393 struct lustre_lnet_ip2nets ip2nets;
3394 struct lustre_lnet_ip_range_descr *ip_range_descr = NULL,
3396 int rc = LUSTRE_CFG_RC_NO_ERR;
3397 struct cfs_expr_list *global_cpts = NULL;
3398 struct cfs_expr_list *el, *el_tmp;
3399 struct lnet_ioctl_config_lnd_tunables tunables;
3400 struct lnet_dlc_intf_descr *intf_descr, *intf_tmp;
3403 memset(&tunables, 0, sizeof(tunables));
3405 /* initialize all lists */
3406 INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
3407 INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
3408 INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
3410 net = cYAML_get_object_item(tree, "net-spec");
3412 return LUSTRE_CFG_RC_BAD_PARAM;
3414 if (net != NULL && net->cy_valuestring == NULL)
3415 return LUSTRE_CFG_RC_BAD_PARAM;
3417 /* assign the network id */
3418 ip2nets.ip2nets_net.nw_id = libcfs_str2net(net->cy_valuestring);
3419 if (ip2nets.ip2nets_net.nw_id == LNET_NID_ANY)
3420 return LUSTRE_CFG_RC_BAD_PARAM;
3422 seq_no = cYAML_get_object_item(tree, "seq_no");
3424 intf = cYAML_get_object_item(tree, "interfaces");
3426 rc = yaml_copy_intf_info(intf, &ip2nets.ip2nets_net);
3428 return LUSTRE_CFG_RC_BAD_PARAM;
3431 ip_range = cYAML_get_object_item(tree, "ip-range");
3432 if (ip_range != NULL) {
3433 item = ip_range->cy_child;
3434 while (item != NULL) {
3435 if (item->cy_valuestring == NULL) {
3436 item = item->cy_next;
3440 rc = lustre_lnet_add_ip_range(&ip2nets.ip2nets_ip_ranges,
3441 item->cy_valuestring);
3443 if (rc != LUSTRE_CFG_RC_NO_ERR)
3446 item = item->cy_next;
3450 found = yaml_extract_tunables(tree, &tunables, &global_cpts,
3451 LNET_NETTYP(ip2nets.ip2nets_net.nw_id));
3453 rc = lustre_lnet_config_ip2nets(&ip2nets,
3454 (found) ? &tunables : NULL,
3456 (seq_no) ? seq_no->cy_valueint : -1,
3460 * don't stop because there was no match. Continue processing the
3461 * rest of the rules. If non-match then nothing is configured
3463 if (rc == LUSTRE_CFG_RC_NO_MATCH)
3464 rc = LUSTRE_CFG_RC_NO_ERR;
3466 list_for_each_entry_safe(intf_descr, intf_tmp,
3467 &ip2nets.ip2nets_net.nw_intflist,
3469 list_del(&intf_descr->intf_on_network);
3470 free_intf_descr(intf_descr);
3473 list_for_each_entry_safe(ip_range_descr, tmp,
3474 &ip2nets.ip2nets_ip_ranges,
3476 list_del(&ip_range_descr->ipr_entry);
3477 list_for_each_entry_safe(el, el_tmp, &ip_range_descr->ipr_expr,
3479 list_del(&el->el_link);
3480 cfs_expr_list_free(el);
3482 free(ip_range_descr);
3488 static int handle_yaml_del_ni(struct cYAML *tree, struct cYAML **show_rc,
3489 struct cYAML **err_rc)
3491 struct cYAML *net = NULL, *intf = NULL, *seq_no = NULL, *item = NULL,
3493 int num_entries, rc;
3494 struct lnet_dlc_network_descr nw_descr;
3496 INIT_LIST_HEAD(&nw_descr.network_on_rule);
3497 INIT_LIST_HEAD(&nw_descr.nw_intflist);
3499 net = cYAML_get_object_item(tree, "net type");
3501 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
3503 local_nis = cYAML_get_object_item(tree, "local NI(s)");
3504 if (local_nis == NULL)
3505 return LUSTRE_CFG_RC_MISSING_PARAM;
3507 if (!cYAML_is_sequence(local_nis))
3508 return LUSTRE_CFG_RC_BAD_PARAM;
3510 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
3511 intf = cYAML_get_object_item(item, "interfaces");
3514 num_entries = yaml_copy_intf_info(intf, &nw_descr);
3515 if (num_entries <= 0) {
3516 cYAML_build_error(num_entries, -1, "ni", "add",
3517 "bad interface list",
3519 return LUSTRE_CFG_RC_BAD_PARAM;
3523 seq_no = cYAML_get_object_item(tree, "seq_no");
3525 rc = lustre_lnet_del_ni((net) ? &nw_descr : NULL,
3526 (seq_no) ? seq_no->cy_valueint : -1,
3532 static int yaml_copy_peer_nids(struct cYAML *tree, char ***nidsppp, bool del)
3534 struct cYAML *nids_entry = NULL, *child = NULL, *entry = NULL,
3537 int num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
3539 prim_nid = cYAML_get_object_item(tree, "primary nid");
3540 if (!prim_nid || !prim_nid->cy_valuestring)
3541 return LUSTRE_CFG_RC_MISSING_PARAM;
3543 nids_entry = cYAML_get_object_item(tree, "peer ni");
3544 if (cYAML_is_sequence(nids_entry)) {
3545 while (cYAML_get_next_seq_item(nids_entry, &child)) {
3546 entry = cYAML_get_object_item(child, "nid");
3547 /* don't count an empty entry */
3548 if (!entry || !entry->cy_valuestring)
3551 if ((strcmp(entry->cy_valuestring, prim_nid->cy_valuestring)
3554 * primary nid is present in the list of
3555 * nids so that means we want to delete
3556 * the entire peer, so no need to go
3557 * further. Just delete the entire peer.
3567 return LUSTRE_CFG_RC_MISSING_PARAM;
3569 nids = calloc(sizeof(*nids) * num, 1);
3571 return LUSTRE_CFG_RC_OUT_OF_MEM;
3573 /* now grab all the nids */
3576 while (cYAML_get_next_seq_item(nids_entry, &child)) {
3577 entry = cYAML_get_object_item(child, "nid");
3578 if (!entry || !entry->cy_valuestring)
3581 nids[num] = calloc(strlen(entry->cy_valuestring) + 1, 1);
3583 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3586 strncpy(nids[num], entry->cy_valuestring,
3587 strlen(entry->cy_valuestring));
3597 yaml_free_string_array(nids, num);
3602 static int handle_yaml_config_peer(struct cYAML *tree, struct cYAML **show_rc,
3603 struct cYAML **err_rc)
3607 struct cYAML *seq_no, *prim_nid, *non_mr;
3609 num = yaml_copy_peer_nids(tree, &nids, false);
3613 seq_no = cYAML_get_object_item(tree, "seq_no");
3614 prim_nid = cYAML_get_object_item(tree, "primary nid");
3615 non_mr = cYAML_get_object_item(tree, "non_mr");
3617 rc = lustre_lnet_config_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
3619 (non_mr) ? false : true,
3620 (seq_no) ? seq_no->cy_valueint : -1,
3623 yaml_free_string_array(nids, num);
3627 static int handle_yaml_del_peer(struct cYAML *tree, struct cYAML **show_rc,
3628 struct cYAML **err_rc)
3632 struct cYAML *seq_no, *prim_nid;
3634 num = yaml_copy_peer_nids(tree, &nids, true);
3638 seq_no = cYAML_get_object_item(tree, "seq_no");
3639 prim_nid = cYAML_get_object_item(tree, "primary nid");
3641 rc = lustre_lnet_del_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
3643 (seq_no) ? seq_no->cy_valueint : -1,
3646 yaml_free_string_array(nids, num);
3650 static int handle_yaml_config_buffers(struct cYAML *tree,
3651 struct cYAML **show_rc,
3652 struct cYAML **err_rc)
3655 struct cYAML *tiny, *small, *large, *seq_no;
3657 tiny = cYAML_get_object_item(tree, "tiny");
3658 small = cYAML_get_object_item(tree, "small");
3659 large = cYAML_get_object_item(tree, "large");
3660 seq_no = cYAML_get_object_item(tree, "seq_no");
3662 rc = lustre_lnet_config_buffers((tiny) ? tiny->cy_valueint : -1,
3663 (small) ? small->cy_valueint : -1,
3664 (large) ? large->cy_valueint : -1,
3665 (seq_no) ? seq_no->cy_valueint : -1,
3671 static int handle_yaml_config_routing(struct cYAML *tree,
3672 struct cYAML **show_rc,
3673 struct cYAML **err_rc)
3675 int rc = LUSTRE_CFG_RC_NO_ERR;
3676 struct cYAML *seq_no, *enable;
3678 seq_no = cYAML_get_object_item(tree, "seq_no");
3679 enable = cYAML_get_object_item(tree, "enable");
3682 rc = lustre_lnet_enable_routing(enable->cy_valueint,
3684 seq_no->cy_valueint : -1,
3691 static int handle_yaml_del_route(struct cYAML *tree, struct cYAML **show_rc,
3692 struct cYAML **err_rc)
3696 struct cYAML *seq_no;
3698 net = cYAML_get_object_item(tree, "net");
3699 gw = cYAML_get_object_item(tree, "gateway");
3700 seq_no = cYAML_get_object_item(tree, "seq_no");
3702 return lustre_lnet_del_route((net) ? net->cy_valuestring : NULL,
3703 (gw) ? gw->cy_valuestring : NULL,
3704 (seq_no) ? seq_no->cy_valueint : -1,
3708 static int handle_yaml_del_routing(struct cYAML *tree, struct cYAML **show_rc,
3709 struct cYAML **err_rc)
3711 struct cYAML *seq_no;
3713 seq_no = cYAML_get_object_item(tree, "seq_no");
3715 return lustre_lnet_enable_routing(0, (seq_no) ?
3716 seq_no->cy_valueint : -1,
3720 static int handle_yaml_show_route(struct cYAML *tree, struct cYAML **show_rc,
3721 struct cYAML **err_rc)
3727 struct cYAML *detail;
3728 struct cYAML *seq_no;
3730 net = cYAML_get_object_item(tree, "net");
3731 gw = cYAML_get_object_item(tree, "gateway");
3732 hop = cYAML_get_object_item(tree, "hop");
3733 prio = cYAML_get_object_item(tree, "priority");
3734 detail = cYAML_get_object_item(tree, "detail");
3735 seq_no = cYAML_get_object_item(tree, "seq_no");
3737 return lustre_lnet_show_route((net) ? net->cy_valuestring : NULL,
3738 (gw) ? gw->cy_valuestring : NULL,
3739 (hop) ? hop->cy_valueint : -1,
3740 (prio) ? prio->cy_valueint : -1,
3741 (detail) ? detail->cy_valueint : 0,
3742 (seq_no) ? seq_no->cy_valueint : -1,
3743 show_rc, err_rc, false);
3746 static int handle_yaml_show_net(struct cYAML *tree, struct cYAML **show_rc,
3747 struct cYAML **err_rc)
3749 struct cYAML *net, *detail, *seq_no;
3751 net = cYAML_get_object_item(tree, "net");
3752 detail = cYAML_get_object_item(tree, "detail");
3753 seq_no = cYAML_get_object_item(tree, "seq_no");
3755 return lustre_lnet_show_net((net) ? net->cy_valuestring : NULL,
3756 (detail) ? detail->cy_valueint : 0,
3757 (seq_no) ? seq_no->cy_valueint : -1,
3758 show_rc, err_rc, false);
3761 static int handle_yaml_show_routing(struct cYAML *tree, struct cYAML **show_rc,
3762 struct cYAML **err_rc)
3764 struct cYAML *seq_no;
3766 seq_no = cYAML_get_object_item(tree, "seq_no");
3768 return lustre_lnet_show_routing((seq_no) ? seq_no->cy_valueint : -1,
3769 show_rc, err_rc, false);
3772 static int handle_yaml_show_peers(struct cYAML *tree, struct cYAML **show_rc,
3773 struct cYAML **err_rc)
3775 struct cYAML *seq_no, *nid, *detail;
3777 seq_no = cYAML_get_object_item(tree, "seq_no");
3778 detail = cYAML_get_object_item(tree, "detail");
3779 nid = cYAML_get_object_item(tree, "nid");
3781 return lustre_lnet_show_peer((nid) ? nid->cy_valuestring : NULL,
3782 (detail) ? detail->cy_valueint : 0,
3783 (seq_no) ? seq_no->cy_valueint : -1,
3784 show_rc, err_rc, false);
3787 static int handle_yaml_show_stats(struct cYAML *tree, struct cYAML **show_rc,
3788 struct cYAML **err_rc)
3790 struct cYAML *seq_no;
3792 seq_no = cYAML_get_object_item(tree, "seq_no");
3794 return lustre_lnet_show_stats((seq_no) ? seq_no->cy_valueint : -1,
3798 static int handle_yaml_config_numa(struct cYAML *tree, struct cYAML **show_rc,
3799 struct cYAML **err_rc)
3801 struct cYAML *seq_no, *range;
3803 seq_no = cYAML_get_object_item(tree, "seq_no");
3804 range = cYAML_get_object_item(tree, "range");
3806 return lustre_lnet_config_numa_range(range ? range->cy_valueint : -1,
3807 seq_no ? seq_no->cy_valueint : -1,
3811 static int handle_yaml_del_numa(struct cYAML *tree, struct cYAML **show_rc,
3812 struct cYAML **err_rc)
3814 struct cYAML *seq_no;
3816 seq_no = cYAML_get_object_item(tree, "seq_no");
3818 return lustre_lnet_config_numa_range(0, seq_no ? seq_no->cy_valueint : -1,
3822 static int handle_yaml_show_numa(struct cYAML *tree, struct cYAML **show_rc,
3823 struct cYAML **err_rc)
3825 struct cYAML *seq_no;
3827 seq_no = cYAML_get_object_item(tree, "seq_no");
3829 return lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint : -1,
3833 static int handle_yaml_config_global_settings(struct cYAML *tree,
3834 struct cYAML **show_rc,
3835 struct cYAML **err_rc)
3837 struct cYAML *max_intf, *numa, *discovery, *seq_no;
3840 seq_no = cYAML_get_object_item(tree, "seq_no");
3841 max_intf = cYAML_get_object_item(tree, "max_intf");
3843 rc = lustre_lnet_config_max_intf(max_intf->cy_valueint,
3844 seq_no ? seq_no->cy_valueint
3848 numa = cYAML_get_object_item(tree, "numa_range");
3850 rc = lustre_lnet_config_numa_range(numa->cy_valueint,
3851 seq_no ? seq_no->cy_valueint
3855 discovery = cYAML_get_object_item(tree, "discovery");
3857 rc = lustre_lnet_config_discovery(discovery->cy_valueint,
3858 seq_no ? seq_no->cy_valueint
3865 static int handle_yaml_del_global_settings(struct cYAML *tree,
3866 struct cYAML **show_rc,
3867 struct cYAML **err_rc)
3869 struct cYAML *max_intf, *numa, *discovery, *seq_no;
3872 seq_no = cYAML_get_object_item(tree, "seq_no");
3873 max_intf = cYAML_get_object_item(tree, "max_intf");
3875 rc = lustre_lnet_config_max_intf(LNET_INTERFACES_MAX_DEFAULT,
3876 seq_no ? seq_no->cy_valueint
3880 numa = cYAML_get_object_item(tree, "numa_range");
3882 rc = lustre_lnet_config_numa_range(0,
3883 seq_no ? seq_no->cy_valueint
3887 /* peer discovery is enabled by default */
3888 discovery = cYAML_get_object_item(tree, "discovery");
3890 rc = lustre_lnet_config_discovery(1,
3891 seq_no ? seq_no->cy_valueint
3898 static int handle_yaml_show_global_settings(struct cYAML *tree,
3899 struct cYAML **show_rc,
3900 struct cYAML **err_rc)
3902 struct cYAML *max_intf, *numa, *discovery, *seq_no;
3905 seq_no = cYAML_get_object_item(tree, "seq_no");
3906 max_intf = cYAML_get_object_item(tree, "max_intf");
3908 rc = lustre_lnet_show_max_intf(seq_no ? seq_no->cy_valueint
3912 numa = cYAML_get_object_item(tree, "numa_range");
3914 rc = lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint
3918 discovery = cYAML_get_object_item(tree, "discovery");
3920 rc = lustre_lnet_show_discovery(seq_no ? seq_no->cy_valueint
3927 static int handle_yaml_ping(struct cYAML *tree, struct cYAML **show_rc,
3928 struct cYAML **err_rc)
3930 struct cYAML *seq_no, *nid, *timeout;
3932 seq_no = cYAML_get_object_item(tree, "seq_no");
3933 nid = cYAML_get_object_item(tree, "primary nid");
3934 timeout = cYAML_get_object_item(tree, "timeout");
3936 return lustre_lnet_ping_nid((nid) ? nid->cy_valuestring : NULL,
3937 (timeout) ? timeout->cy_valueint : 1000,
3938 (seq_no) ? seq_no->cy_valueint : -1,
3942 static int handle_yaml_discover(struct cYAML *tree, struct cYAML **show_rc,
3943 struct cYAML **err_rc)
3945 struct cYAML *seq_no, *nid, *force;
3947 seq_no = cYAML_get_object_item(tree, "seq_no");
3948 nid = cYAML_get_object_item(tree, "primary nid");
3949 force = cYAML_get_object_item(tree, "force");
3951 return lustre_lnet_discover_nid((nid) ? nid->cy_valuestring : NULL,
3952 (force) ? force->cy_valueint : 0,
3953 (seq_no) ? seq_no->cy_valueint : -1,
3957 static int handle_yaml_no_op()
3959 return LUSTRE_CFG_RC_NO_ERR;
3962 struct lookup_cmd_hdlr_tbl {
3967 static struct lookup_cmd_hdlr_tbl lookup_config_tbl[] = {
3968 { .name = "route", .cb = handle_yaml_config_route },
3969 { .name = "net", .cb = handle_yaml_config_ni },
3970 { .name = "ip2nets", .cb = handle_yaml_config_ip2nets },
3971 { .name = "peer", .cb = handle_yaml_config_peer },
3972 { .name = "routing", .cb = handle_yaml_config_routing },
3973 { .name = "buffers", .cb = handle_yaml_config_buffers },
3974 { .name = "statistics", .cb = handle_yaml_no_op },
3975 { .name = "global", .cb = handle_yaml_config_global_settings},
3976 { .name = "numa", .cb = handle_yaml_config_numa },
3977 { .name = "ping", .cb = handle_yaml_no_op },
3978 { .name = "discover", .cb = handle_yaml_no_op },
3981 static struct lookup_cmd_hdlr_tbl lookup_del_tbl[] = {
3982 { .name = "route", .cb = handle_yaml_del_route },
3983 { .name = "net", .cb = handle_yaml_del_ni },
3984 { .name = "ip2nets", .cb = handle_yaml_no_op },
3985 { .name = "peer", .cb = handle_yaml_del_peer },
3986 { .name = "routing", .cb = handle_yaml_del_routing },
3987 { .name = "buffers", .cb = handle_yaml_no_op },
3988 { .name = "statistics", .cb = handle_yaml_no_op },
3989 { .name = "global", .cb = handle_yaml_del_global_settings},
3990 { .name = "numa", .cb = handle_yaml_del_numa },
3991 { .name = "ping", .cb = handle_yaml_no_op },
3992 { .name = "discover", .cb = handle_yaml_no_op },
3995 static struct lookup_cmd_hdlr_tbl lookup_show_tbl[] = {
3996 { .name = "route", .cb = handle_yaml_show_route },
3997 { .name = "net", .cb = handle_yaml_show_net },
3998 { .name = "peer", .cb = handle_yaml_show_peers },
3999 { .name = "ip2nets", .cb = handle_yaml_no_op },
4000 { .name = "routing", .cb = handle_yaml_show_routing },
4001 { .name = "buffers", .cb = handle_yaml_show_routing },
4002 { .name = "statistics", .cb = handle_yaml_show_stats },
4003 { .name = "global", .cb = handle_yaml_show_global_settings},
4004 { .name = "numa", .cb = handle_yaml_show_numa },
4005 { .name = "ping", .cb = handle_yaml_no_op },
4006 { .name = "discover", .cb = handle_yaml_no_op },
4009 static struct lookup_cmd_hdlr_tbl lookup_exec_tbl[] = {
4010 { .name = "route", .cb = handle_yaml_no_op },
4011 { .name = "net", .cb = handle_yaml_no_op },
4012 { .name = "peer", .cb = handle_yaml_no_op },
4013 { .name = "ip2nets", .cb = handle_yaml_no_op },
4014 { .name = "routing", .cb = handle_yaml_no_op },
4015 { .name = "buffers", .cb = handle_yaml_no_op },
4016 { .name = "statistics", .cb = handle_yaml_no_op },
4017 { .name = "global", .cb = handle_yaml_no_op },
4018 { .name = "numa", .cb = handle_yaml_no_op },
4019 { .name = "ping", .cb = handle_yaml_ping },
4020 { .name = "discover", .cb = handle_yaml_discover },
4023 static cmd_handler_t lookup_fn(char *key,
4024 struct lookup_cmd_hdlr_tbl *tbl)
4030 for (i = 0; tbl[i].name != NULL; i++) {
4031 if (strncmp(key, tbl[i].name, strlen(tbl[i].name)) == 0)
4038 static int lustre_yaml_cb_helper(char *f, struct lookup_cmd_hdlr_tbl *table,
4039 struct cYAML **show_rc, struct cYAML **err_rc)
4041 struct cYAML *tree, *item = NULL, *head, *child;
4043 char err_str[LNET_MAX_STR_LEN];
4044 int rc = LUSTRE_CFG_RC_NO_ERR, return_rc = LUSTRE_CFG_RC_NO_ERR;
4046 tree = cYAML_build_tree(f, NULL, 0, err_rc, false);
4048 return LUSTRE_CFG_RC_BAD_PARAM;
4050 child = tree->cy_child;
4051 while (child != NULL) {
4052 cb = lookup_fn(child->cy_string, table);
4054 snprintf(err_str, sizeof(err_str),
4055 "\"call back for '%s' not found\"",
4057 cYAML_build_error(LUSTRE_CFG_RC_BAD_PARAM, -1,
4058 "yaml", "helper", err_str, err_rc);
4062 if (cYAML_is_sequence(child)) {
4063 while ((head = cYAML_get_next_seq_item(child, &item))
4065 rc = cb(head, show_rc, err_rc);
4066 if (rc != LUSTRE_CFG_RC_NO_ERR)
4070 rc = cb(child, show_rc, err_rc);
4071 if (rc != LUSTRE_CFG_RC_NO_ERR)
4075 child = child->cy_next;
4079 cYAML_free_tree(tree);
4084 int lustre_yaml_config(char *f, struct cYAML **err_rc)
4086 return lustre_yaml_cb_helper(f, lookup_config_tbl,
4090 int lustre_yaml_del(char *f, struct cYAML **err_rc)
4092 return lustre_yaml_cb_helper(f, lookup_del_tbl,
4096 int lustre_yaml_show(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
4098 return lustre_yaml_cb_helper(f, lookup_show_tbl,
4102 int lustre_yaml_exec(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
4104 return lustre_yaml_cb_helper(f, lookup_exec_tbl,