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;
819 __u32 rnet = LNET_NIDNET(LNET_NID_ANY);
820 __u32 net = LNET_NIDNET(LNET_NID_ANY);
821 char err_str[LNET_MAX_STR_LEN];
822 __u32 ip_list[MAX_NUM_IPS];
823 struct lustre_lnet_ip2nets ip2nets;
825 /* initialize all lists */
826 INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
827 INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
828 INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
830 snprintf(err_str, sizeof(err_str), "\"Success\"");
832 if (nw == NULL || gw == NULL) {
835 "\"missing mandatory parameter in route config:'%s'\"",
836 (nw == NULL && gw == NULL) ? "network, gateway" :
837 (nw == NULL) ? "network" : "gateway");
838 rc = LUSTRE_CFG_RC_MISSING_PARAM;
842 rnet = libcfs_str2net(nw);
843 if (rnet == LNET_NIDNET(LNET_NID_ANY)) {
846 "\"cannot parse remote net %s\"", nw);
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 rc = lnet_expr2ips(gw, ip_list,
875 &ip2nets, &net, err_str);
876 if (rc == LUSTRE_CFG_RC_LAST_ELEM)
878 else if (rc < LUSTRE_CFG_RC_NO_ERR)
883 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
885 data.cfg_config_u.cfg_route.rtr_hop = hops;
886 data.cfg_config_u.cfg_route.rtr_priority = prio;
888 for (i = MAX_NUM_IPS - 1; i > ip_idx; i--) {
889 gateway_nid = LNET_MKNID(net, ip_list[i]);
890 if (gateway_nid == LNET_NID_ANY) {
893 "\"cannot form gateway NID: %u\"",
895 err_str[LNET_MAX_STR_LEN - 1] = '\0';
896 rc = LUSTRE_CFG_RC_BAD_PARAM;
899 data.cfg_nid = gateway_nid;
901 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
906 "\"cannot add route: %s\"", strerror(errno));
911 cYAML_build_error(rc, seq_no, ADD_CMD, "route", err_str, err_rc);
916 int lustre_lnet_del_route(char *nw, char *gw,
917 int seq_no, struct cYAML **err_rc)
919 struct lnet_ioctl_config_data data;
920 lnet_nid_t gateway_nid;
921 int rc = LUSTRE_CFG_RC_NO_ERR;
922 __u32 rnet = LNET_NIDNET(LNET_NID_ANY);
923 __u32 net = LNET_NIDNET(LNET_NID_ANY);
924 char err_str[LNET_MAX_STR_LEN];
926 __u32 ip_list[MAX_NUM_IPS];
927 struct lustre_lnet_ip2nets ip2nets;
929 /* initialize all lists */
930 INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
931 INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
932 INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
934 snprintf(err_str, sizeof(err_str), "\"Success\"");
936 if (nw == NULL || gw == NULL) {
939 "\"missing mandatory parameter in route delete: '%s'\"",
940 (nw == NULL && gw == NULL) ? "network, gateway" :
941 (nw == NULL) ? "network" : "gateway");
942 rc = LUSTRE_CFG_RC_MISSING_PARAM;
946 rnet = libcfs_str2net(nw);
947 if (rnet == LNET_NIDNET(LNET_NID_ANY)) {
950 "\"cannot parse remote net '%s'\"", nw);
951 rc = LUSTRE_CFG_RC_BAD_PARAM;
955 rc = lnet_expr2ips(gw, ip_list,
956 &ip2nets, &net, err_str);
957 if (rc == LUSTRE_CFG_RC_LAST_ELEM)
959 else if (rc < LUSTRE_CFG_RC_NO_ERR)
964 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
967 for (i = MAX_NUM_IPS - 1; i > ip_idx; i--) {
968 gateway_nid = LNET_MKNID(net, ip_list[i]);
969 if (gateway_nid == LNET_NID_ANY) {
972 "\"cannot form gateway NID: %u\"",
974 err_str[LNET_MAX_STR_LEN - 1] = '\0';
975 rc = LUSTRE_CFG_RC_BAD_PARAM;
978 data.cfg_nid = gateway_nid;
980 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
985 "\"cannot delete route: %s\"", strerror(errno));
990 cYAML_build_error(rc, seq_no, DEL_CMD, "route", err_str, err_rc);
995 int lustre_lnet_show_route(char *nw, char *gw, int hops, int prio, int detail,
996 int seq_no, struct cYAML **show_rc,
997 struct cYAML **err_rc, bool backup)
999 struct lnet_ioctl_config_data data;
1000 lnet_nid_t gateway_nid;
1001 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1003 __u32 net = LNET_NIDNET(LNET_NID_ANY);
1005 struct cYAML *root = NULL, *route = NULL, *item = NULL;
1006 struct cYAML *first_seq = NULL;
1007 char err_str[LNET_MAX_STR_LEN];
1010 snprintf(err_str, sizeof(err_str),
1011 "\"out of memory\"");
1014 net = libcfs_str2net(nw);
1015 if (net == LNET_NIDNET(LNET_NID_ANY)) {
1018 "\"cannot parse net '%s'\"", nw);
1019 rc = LUSTRE_CFG_RC_BAD_PARAM;
1024 /* show all routes without filtering on net */
1025 net = LNET_NIDNET(LNET_NID_ANY);
1029 gateway_nid = libcfs_str2nid(gw);
1030 if (gateway_nid == LNET_NID_ANY) {
1033 "\"cannot parse gateway NID '%s'\"", gw);
1034 rc = LUSTRE_CFG_RC_BAD_PARAM;
1038 /* show all routes with out filtering on gateway */
1039 gateway_nid = LNET_NID_ANY;
1041 if ((hops < 1 && hops != -1) || hops > 255) {
1044 "\"invalid hop count %d, must be between 0 and 256\"",
1046 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1050 /* create struct cYAML root object */
1051 root = cYAML_create_object(NULL, NULL);
1055 route = cYAML_create_seq(root, "route");
1060 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1063 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
1069 /* filter on provided data */
1070 if (net != LNET_NIDNET(LNET_NID_ANY) &&
1071 net != data.cfg_net)
1074 if (gateway_nid != LNET_NID_ANY &&
1075 gateway_nid != data.cfg_nid)
1079 hops != data.cfg_config_u.cfg_route.rtr_hop)
1083 prio != data.cfg_config_u.cfg_route.rtr_priority)
1086 /* default rc to -1 incase we hit the goto */
1090 item = cYAML_create_seq_item(route);
1094 if (first_seq == NULL)
1097 if (cYAML_create_string(item, "net",
1098 libcfs_net2str(data.cfg_net)) == NULL)
1101 if (cYAML_create_string(item, "gateway",
1102 libcfs_nid2str(data.cfg_nid)) == NULL)
1106 if (cYAML_create_number(item, "hop",
1107 (int) data.cfg_config_u.
1108 cfg_route.rtr_hop) ==
1112 if (cYAML_create_number(item, "priority",
1114 cfg_route.rtr_priority) == NULL)
1118 cYAML_create_string(item, "state",
1119 data.cfg_config_u.cfg_route.
1121 "up" : "down") == NULL)
1126 /* print output iff show_rc is not provided */
1127 if (show_rc == NULL)
1128 cYAML_print_tree(root);
1130 if (l_errno != ENOENT) {
1133 "\"cannot get routes: %s\"",
1138 rc = LUSTRE_CFG_RC_NO_ERR;
1140 snprintf(err_str, sizeof(err_str), "\"success\"");
1142 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
1143 cYAML_free_tree(root);
1144 } else if (show_rc != NULL && *show_rc != NULL) {
1145 struct cYAML *show_node;
1146 /* find the route node, if one doesn't exist then
1147 * insert one. Otherwise add to the one there
1149 show_node = cYAML_get_object_item(*show_rc, "route");
1150 if (show_node != NULL && cYAML_is_sequence(show_node)) {
1151 cYAML_insert_child(show_node, first_seq);
1154 } else if (show_node == NULL) {
1155 cYAML_insert_sibling((*show_rc)->cy_child,
1159 cYAML_free_tree(root);
1165 cYAML_build_error(rc, seq_no, SHOW_CMD, "route", err_str, err_rc);
1170 static int socket_intf_query(int request, char *intf,
1176 if (strlen(intf) >= IFNAMSIZ || ifr == NULL)
1177 return LUSTRE_CFG_RC_BAD_PARAM;
1179 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
1181 return LUSTRE_CFG_RC_BAD_PARAM;
1183 strcpy(ifr->ifr_name, intf);
1184 rc = ioctl(sockfd, request, ifr);
1186 rc = LUSTRE_CFG_RC_BAD_PARAM;
1193 static int lustre_lnet_queryip(struct lnet_dlc_intf_descr *intf, __u32 *ip)
1198 memset(&ifr, 0, sizeof(ifr));
1199 rc = socket_intf_query(SIOCGIFFLAGS, intf->intf_name, &ifr);
1201 return LUSTRE_CFG_RC_BAD_PARAM;
1203 if ((ifr.ifr_flags & IFF_UP) == 0)
1204 return LUSTRE_CFG_RC_BAD_PARAM;
1206 memset(&ifr, 0, sizeof(ifr));
1207 rc = socket_intf_query(SIOCGIFADDR, intf->intf_name, &ifr);
1209 return LUSTRE_CFG_RC_BAD_PARAM;
1211 *ip = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
1212 *ip = bswap_32(*ip);
1214 return LUSTRE_CFG_RC_NO_ERR;
1218 * for each interface in the array of interfaces find the IP address of
1219 * that interface, create its nid and add it to an array of NIDs.
1220 * Stop if any of the interfaces is down
1222 static int lustre_lnet_intf2nids(struct lnet_dlc_network_descr *nw,
1223 lnet_nid_t **nids, __u32 *nnids,
1224 char *err_str, size_t str_len)
1226 int i = 0, count = 0, rc;
1227 struct lnet_dlc_intf_descr *intf;
1228 char val[LNET_MAX_STR_LEN];
1233 if (nw == NULL || nids == NULL) {
1234 snprintf(err_str, str_len,
1235 "\"unexpected parameters to lustre_lnet_intf2nids()\"");
1236 err_str[str_len - 1] = '\0';
1237 return LUSTRE_CFG_RC_BAD_PARAM;
1240 if (LNET_NETTYP(nw->nw_id) == GNILND) {
1243 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network)
1247 *nids = calloc(count, sizeof(lnet_nid_t));
1248 if (*nids == NULL) {
1249 snprintf(err_str, str_len,
1250 "\"out of memory\"");
1251 err_str[str_len - 1] = '\0';
1252 return LUSTRE_CFG_RC_OUT_OF_MEM;
1255 * special case the GNI interface since it doesn't have an IP
1256 * address. The assumption is that there can only be one GNI
1257 * interface in the system. No interface name is provided.
1259 if (LNET_NETTYP(nw->nw_id) == GNILND) {
1260 rc = read_sysfs_file(gni_nid_path, "nid", val,
1263 snprintf(err_str, str_len,
1264 "\"cannot read gni nid\"");
1265 err_str[str_len - 1] = '\0';
1268 gni_num = atoi(val);
1270 (*nids)[i] = LNET_MKNID(nw->nw_id, gni_num);
1275 /* look at the other interfaces */
1276 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network) {
1277 rc = lustre_lnet_queryip(intf, &ip);
1278 if (rc != LUSTRE_CFG_RC_NO_ERR) {
1279 snprintf(err_str, str_len,
1280 "\"couldn't query intf %s\"", intf->intf_name);
1281 err_str[str_len - 1] = '\0';
1284 (*nids)[i] = LNET_MKNID(nw->nw_id, ip);
1300 * called repeatedly until a match or no more ip range
1302 * ip_range expression
1303 * interface list with all the interface names.
1304 * all the interfaces in the system.
1306 * try to match the ip_range expr to one of the interfaces' IPs in
1307 * the system. If we hit a patch for an interface. Check if that
1308 * interface name is in the list.
1310 * If there are more than one interface in the list, then make sure
1311 * that the IPs for all of these interfaces match the ip ranges
1314 * for each interface in intf_list
1315 * look up the intf name in ifa
1316 * if not there then no match
1317 * check ip obtained from ifa against a match to any of the
1319 * If no match, then fail
1321 * The result is that all the interfaces have to match.
1323 int lustre_lnet_match_ip_to_intf(struct ifaddrs *ifa,
1324 struct list_head *intf_list,
1325 struct list_head *ip_ranges)
1329 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1330 struct ifaddrs *ifaddr = ifa;
1331 struct lustre_lnet_ip_range_descr *ip_range;
1335 * if there are no explicit interfaces, and no ip ranges, then
1336 * configure the first tcp interface we encounter.
1338 if (list_empty(intf_list) && list_empty(ip_ranges)) {
1339 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1340 if (ifaddr->ifa_addr == NULL)
1343 if ((ifaddr->ifa_flags & IFF_UP) == 0)
1346 family = ifaddr->ifa_addr->sa_family;
1347 if (family == AF_INET &&
1348 strcmp(ifaddr->ifa_name, "lo") != 0) {
1349 rc = lustre_lnet_add_intf_descr
1350 (intf_list, ifaddr->ifa_name,
1351 strlen(ifaddr->ifa_name));
1353 if (rc != LUSTRE_CFG_RC_NO_ERR)
1356 return LUSTRE_CFG_RC_MATCH;
1359 return LUSTRE_CFG_RC_NO_MATCH;
1363 * First interface which matches an IP pattern will be used
1365 if (list_empty(intf_list)) {
1367 * no interfaces provided in the rule, but an ip range is
1368 * provided, so try and match an interface to the ip
1371 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1372 if (ifaddr->ifa_addr == NULL)
1375 if ((ifaddr->ifa_flags & IFF_UP) == 0)
1378 family = ifaddr->ifa_addr->sa_family;
1379 if (family == AF_INET) {
1380 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->
1383 list_for_each_entry(ip_range, ip_ranges,
1385 rc = cfs_ip_addr_match(bswap_32(ip),
1386 &ip_range->ipr_expr);
1390 rc = lustre_lnet_add_intf_descr
1391 (intf_list, ifaddr->ifa_name,
1392 strlen(ifaddr->ifa_name));
1394 if (rc != LUSTRE_CFG_RC_NO_ERR)
1400 if (!list_empty(intf_list))
1401 return LUSTRE_CFG_RC_MATCH;
1403 return LUSTRE_CFG_RC_NO_MATCH;
1407 * If an interface is explicitly specified the ip-range might or
1408 * might not be specified. if specified the interface needs to match the
1409 * ip-range. If no ip-range then the interfaces are
1410 * automatically matched if they are all up.
1411 * If > 1 interfaces all the interfaces must match for the NI to
1414 list_for_each_entry_safe(intf_descr, tmp, intf_list, intf_on_network) {
1415 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1416 if (ifaddr->ifa_addr == NULL)
1419 family = ifaddr->ifa_addr->sa_family;
1420 if (family == AF_INET &&
1421 strcmp(intf_descr->intf_name,
1422 ifaddr->ifa_name) == 0)
1426 if (ifaddr == NULL) {
1427 list_del(&intf_descr->intf_on_network);
1428 free_intf_descr(intf_descr);
1432 if ((ifaddr->ifa_flags & IFF_UP) == 0) {
1433 list_del(&intf_descr->intf_on_network);
1434 free_intf_descr(intf_descr);
1438 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr.s_addr;
1441 list_for_each_entry(ip_range, ip_ranges, ipr_entry) {
1442 rc = cfs_ip_addr_match(bswap_32(ip), &ip_range->ipr_expr);
1448 /* no match for this interface */
1449 list_del(&intf_descr->intf_on_network);
1450 free_intf_descr(intf_descr);
1454 return LUSTRE_CFG_RC_MATCH;
1457 static int lustre_lnet_resolve_ip2nets_rule(struct lustre_lnet_ip2nets *ip2nets,
1458 lnet_nid_t **nids, __u32 *nnids,
1459 char *err_str, size_t str_len)
1461 struct ifaddrs *ifa;
1462 int rc = LUSTRE_CFG_RC_NO_ERR;
1464 rc = getifaddrs(&ifa);
1466 snprintf(err_str, str_len,
1467 "\"failed to get interface addresses: %d\"", -errno);
1468 err_str[str_len - 1] = '\0';
1472 rc = lustre_lnet_match_ip_to_intf(ifa,
1473 &ip2nets->ip2nets_net.nw_intflist,
1474 &ip2nets->ip2nets_ip_ranges);
1475 if (rc != LUSTRE_CFG_RC_MATCH) {
1476 snprintf(err_str, str_len,
1477 "\"couldn't match ip to existing interfaces\"");
1478 err_str[str_len - 1] = '\0';
1483 rc = lustre_lnet_intf2nids(&ip2nets->ip2nets_net, nids, nnids,
1484 err_str, sizeof(err_str));
1485 if (rc != LUSTRE_CFG_RC_NO_ERR) {
1496 lustre_lnet_ioctl_config_ni(struct list_head *intf_list,
1497 struct lnet_ioctl_config_lnd_tunables *tunables,
1498 struct cfs_expr_list *global_cpts,
1499 lnet_nid_t *nids, char *err_str)
1502 struct lnet_ioctl_config_ni *conf;
1503 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1504 int rc = LUSTRE_CFG_RC_NO_ERR, i = 0;
1507 struct lnet_dlc_intf_descr *intf_descr;
1509 struct cfs_expr_list *cpt_expr;
1511 list_for_each_entry(intf_descr, intf_list,
1513 if (tunables != NULL)
1514 len = sizeof(struct lnet_ioctl_config_ni) +
1515 sizeof(struct lnet_ioctl_config_lnd_tunables);
1517 len = sizeof(struct lnet_ioctl_config_ni);
1519 data = calloc(1, len);
1521 return LUSTRE_CFG_RC_OUT_OF_MEM;
1522 conf = (struct lnet_ioctl_config_ni*) data;
1523 if (tunables != NULL)
1524 tun = (struct lnet_ioctl_config_lnd_tunables*)
1527 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1528 conf->lic_cfg_hdr.ioc_len = len;
1529 conf->lic_nid = nids[i];
1530 strncpy(conf->lic_ni_intf[0], intf_descr->intf_name,
1533 if (intf_descr->cpt_expr != NULL)
1534 cpt_expr = intf_descr->cpt_expr;
1535 else if (global_cpts != NULL)
1536 cpt_expr = global_cpts;
1540 if (cpt_expr != NULL) {
1541 count = cfs_expr_list_values(cpt_expr,
1542 LNET_MAX_SHOW_NUM_CPT,
1545 memcpy(conf->lic_cpts, cpt_array,
1546 sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1555 conf->lic_ncpts = count;
1557 if (tunables != NULL)
1558 memcpy(tun, tunables, sizeof(*tunables));
1560 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1565 "\"cannot add network: %s\"", strerror(errno));
1573 return LUSTRE_CFG_RC_NO_ERR;
1577 lustre_lnet_config_ip2nets(struct lustre_lnet_ip2nets *ip2nets,
1578 struct lnet_ioctl_config_lnd_tunables *tunables,
1579 struct cfs_expr_list *global_cpts,
1580 int seq_no, struct cYAML **err_rc)
1582 lnet_nid_t *nids = NULL;
1585 char err_str[LNET_MAX_STR_LEN];
1587 snprintf(err_str, sizeof(err_str), "\"success\"");
1592 "\"incomplete ip2nets information\"");
1593 rc = LUSTRE_CFG_RC_BAD_PARAM;
1598 * call below function to resolve the rules into a list of nids.
1599 * The memory is allocated in that function then freed here when
1600 * it's no longer needed.
1602 rc = lustre_lnet_resolve_ip2nets_rule(ip2nets, &nids, &nnids, err_str,
1604 if (rc != LUSTRE_CFG_RC_NO_ERR && rc != LUSTRE_CFG_RC_MATCH)
1607 if (list_empty(&ip2nets->ip2nets_net.nw_intflist)) {
1608 snprintf(err_str, sizeof(err_str),
1609 "\"no interfaces match ip2nets rules\"");
1613 rc = lustre_lnet_ioctl_config_ni(&ip2nets->ip2nets_net.nw_intflist,
1614 tunables, global_cpts, nids,
1621 cYAML_build_error(rc, seq_no, ADD_CMD, "ip2nets", err_str, err_rc);
1625 int lustre_lnet_config_ni(struct lnet_dlc_network_descr *nw_descr,
1626 struct cfs_expr_list *global_cpts,
1628 struct lnet_ioctl_config_lnd_tunables *tunables,
1629 int seq_no, struct cYAML **err_rc)
1632 struct lnet_ioctl_config_ni *conf;
1633 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1634 char buf[LNET_MAX_STR_LEN];
1635 int rc = LUSTRE_CFG_RC_NO_ERR;
1636 char err_str[LNET_MAX_STR_LEN];
1637 lnet_nid_t *nids = NULL;
1641 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1644 snprintf(err_str, sizeof(err_str), "\"success\"");
1646 if (ip2net == NULL && (nw_descr == NULL || nw_descr->nw_id == 0 ||
1647 (list_empty(&nw_descr->nw_intflist) &&
1648 LNET_NETTYP(nw_descr->nw_id) != GNILND))) {
1651 "\"missing mandatory parameters in NI config: '%s'\"",
1652 (nw_descr == NULL) ? "network , interface" :
1653 (nw_descr->nw_id == 0) ? "network" : "interface");
1654 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1658 if (ip2net != NULL && strlen(ip2net) >= sizeof(buf)) {
1661 "\"ip2net string too long %d\"",
1662 (int)strlen(ip2net));
1663 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1667 if (ip2net != NULL) {
1668 if (tunables != NULL)
1669 len = sizeof(struct lnet_ioctl_config_ni) +
1670 sizeof(struct lnet_ioctl_config_lnd_tunables);
1672 len = sizeof(struct lnet_ioctl_config_ni);
1673 data = calloc(1, len);
1675 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1678 conf = (struct lnet_ioctl_config_ni*) data;
1679 if (tunables != NULL)
1680 tun = (struct lnet_ioctl_config_lnd_tunables*)
1681 (data + sizeof(*conf));
1683 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1684 conf->lic_cfg_hdr.ioc_len = len;
1685 strncpy(conf->lic_legacy_ip2nets, ip2net,
1688 if (global_cpts != NULL) {
1689 count = cfs_expr_list_values(global_cpts,
1690 LNET_MAX_SHOW_NUM_CPT,
1693 memcpy(conf->lic_cpts, cpt_array,
1694 sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1703 conf->lic_ncpts = count;
1705 if (tunables != NULL)
1706 memcpy(tun, tunables, sizeof(*tunables));
1708 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1713 "\"cannot add network: %s\"", strerror(errno));
1720 if (LNET_NETTYP(nw_descr->nw_id) == LOLND) {
1721 rc = LUSTRE_CFG_RC_NO_ERR;
1725 if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1728 "\"cannot parse net '%s'\"",
1729 libcfs_net2str(nw_descr->nw_id));
1730 rc = LUSTRE_CFG_RC_BAD_PARAM;
1735 * special case the GNI since no interface name is expected
1737 if (list_empty(&nw_descr->nw_intflist) &&
1738 (LNET_NETTYP(nw_descr->nw_id) != GNILND)) {
1741 "\"no interface name provided\"");
1742 rc = LUSTRE_CFG_RC_BAD_PARAM;
1746 rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids,
1747 err_str, sizeof(err_str));
1749 rc = LUSTRE_CFG_RC_BAD_PARAM;
1753 rc = lustre_lnet_ioctl_config_ni(&nw_descr->nw_intflist,
1754 tunables, global_cpts, nids,
1758 if (nw_descr != NULL) {
1759 list_for_each_entry_safe(intf_descr, tmp,
1760 &nw_descr->nw_intflist,
1762 list_del(&intf_descr->intf_on_network);
1763 free_intf_descr(intf_descr);
1767 cYAML_build_error(rc, seq_no, ADD_CMD, "net", err_str, err_rc);
1778 int lustre_lnet_del_ni(struct lnet_dlc_network_descr *nw_descr,
1779 int seq_no, struct cYAML **err_rc)
1781 struct lnet_ioctl_config_ni data;
1782 int rc = LUSTRE_CFG_RC_NO_ERR, i;
1783 char err_str[LNET_MAX_STR_LEN];
1784 lnet_nid_t *nids = NULL;
1786 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1788 snprintf(err_str, sizeof(err_str), "\"success\"");
1790 if (nw_descr == NULL || nw_descr->nw_id == 0) {
1793 "\"missing mandatory parameter in deleting NI: '%s'\"",
1794 (nw_descr == NULL) ? "network , interface" :
1795 (nw_descr->nw_id == 0) ? "network" : "interface");
1796 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1800 if (LNET_NETTYP(nw_descr->nw_id) == LOLND)
1801 return LUSTRE_CFG_RC_NO_ERR;
1803 if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1806 "\"cannot parse net '%s'\"",
1807 libcfs_net2str(nw_descr->nw_id));
1808 rc = LUSTRE_CFG_RC_BAD_PARAM;
1812 rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids,
1813 err_str, sizeof(err_str));
1815 rc = LUSTRE_CFG_RC_BAD_PARAM;
1820 * no interfaces just the nw_id is specified
1823 nids = calloc(1, sizeof(*nids));
1825 snprintf(err_str, sizeof(err_str),
1826 "\"out of memory\"");
1827 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1830 nids[0] = LNET_MKNID(nw_descr->nw_id, 0);
1834 for (i = 0; i < nnids; i++) {
1835 LIBCFS_IOC_INIT_V2(data, lic_cfg_hdr);
1836 data.lic_nid = nids[i];
1838 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_LOCAL_NI, &data);
1843 "\"cannot del network: %s\"", strerror(errno));
1847 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
1849 list_del(&intf_descr->intf_on_network);
1850 free_intf_descr(intf_descr);
1854 cYAML_build_error(rc, seq_no, DEL_CMD, "net", err_str, err_rc);
1863 add_msg_stats_to_yaml_blk(struct cYAML *yaml,
1864 struct lnet_ioctl_comm_count *counts)
1866 if (cYAML_create_number(yaml, "put",
1867 counts->ico_put_count)
1870 if (cYAML_create_number(yaml, "get",
1871 counts->ico_get_count)
1874 if (cYAML_create_number(yaml, "reply",
1875 counts->ico_reply_count)
1878 if (cYAML_create_number(yaml, "ack",
1879 counts->ico_ack_count)
1882 if (cYAML_create_number(yaml, "hello",
1883 counts->ico_hello_count)
1890 static struct lnet_ioctl_comm_count *
1891 get_counts(struct lnet_ioctl_element_msg_stats *msg_stats, int idx)
1894 return &msg_stats->im_send_stats;
1896 return &msg_stats->im_recv_stats;
1898 return &msg_stats->im_drop_stats;
1903 int lustre_lnet_show_net(char *nw, int detail, int seq_no,
1904 struct cYAML **show_rc, struct cYAML **err_rc,
1908 struct lnet_ioctl_config_ni *ni_data;
1909 struct lnet_ioctl_config_lnd_tunables *lnd;
1910 struct lnet_ioctl_element_stats *stats;
1911 struct lnet_ioctl_element_msg_stats msg_stats;
1912 __u32 net = LNET_NIDNET(LNET_NID_ANY);
1913 __u32 prev_net = LNET_NIDNET(LNET_NID_ANY);
1914 int rc = LUSTRE_CFG_RC_OUT_OF_MEM, i, j;
1916 struct cYAML *root = NULL, *tunables = NULL,
1917 *net_node = NULL, *interfaces = NULL,
1918 *item = NULL, *first_seq = NULL,
1919 *tmp = NULL, *statistics = NULL;
1920 int str_buf_len = LNET_MAX_SHOW_NUM_CPT * 2;
1921 char str_buf[str_buf_len];
1923 char err_str[LNET_MAX_STR_LEN];
1924 bool exist = false, new_net = true;
1926 size_t buf_size = sizeof(*ni_data) + sizeof(*lnd) + sizeof(*stats);
1928 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1930 buf = calloc(1, buf_size);
1934 ni_data = (struct lnet_ioctl_config_ni *)buf;
1937 net = libcfs_str2net(nw);
1938 if (net == LNET_NIDNET(LNET_NID_ANY)) {
1941 "\"cannot parse net '%s'\"", nw);
1942 rc = LUSTRE_CFG_RC_BAD_PARAM;
1947 root = cYAML_create_object(NULL, NULL);
1951 net_node = cYAML_create_seq(root, "net");
1952 if (net_node == NULL)
1959 memset(buf, 0, buf_size);
1961 LIBCFS_IOC_INIT_V2(*ni_data, lic_cfg_hdr);
1963 * set the ioc_len to the proper value since INIT assumes
1966 ni_data->lic_cfg_hdr.ioc_len = buf_size;
1967 ni_data->lic_idx = i;
1969 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LOCAL_NI, ni_data);
1975 rc_net = LNET_NIDNET(ni_data->lic_nid);
1977 /* filter on provided data */
1978 if (net != LNET_NIDNET(LNET_NID_ANY) &&
1982 /* if we're backing up don't store lo */
1983 if (backup && LNET_NETTYP(rc_net) == LOLND)
1986 /* default rc to -1 in case we hit the goto */
1990 stats = (struct lnet_ioctl_element_stats *)ni_data->lic_bulk;
1991 lnd = (struct lnet_ioctl_config_lnd_tunables *)
1992 (ni_data->lic_bulk + sizeof(*stats));
1994 if (rc_net != prev_net) {
2001 if (!cYAML_create_string(net_node, "net type",
2002 libcfs_net2str(rc_net)))
2005 tmp = cYAML_create_seq(net_node, "local NI(s)");
2011 /* create the tree to be printed. */
2012 item = cYAML_create_seq_item(tmp);
2016 if (first_seq == NULL)
2020 cYAML_create_string(item, "nid",
2021 libcfs_nid2str(ni_data->lic_nid)) == NULL)
2025 cYAML_create_string(item,
2027 (ni_data->lic_status ==
2028 LNET_NI_STATUS_UP) ?
2029 "up" : "down") == NULL)
2032 /* don't add interfaces unless there is at least one
2034 if (strlen(ni_data->lic_ni_intf[0]) > 0) {
2035 interfaces = cYAML_create_object(item, "interfaces");
2036 if (interfaces == NULL)
2039 for (j = 0; j < LNET_INTERFACES_NUM; j++) {
2040 if (strlen(ni_data->lic_ni_intf[j]) > 0) {
2042 sizeof(str_buf), "%d", j);
2043 if (cYAML_create_string(interfaces,
2045 ni_data->lic_ni_intf[j]) ==
2057 goto continue_without_msg_stats;
2059 statistics = cYAML_create_object(item, "statistics");
2060 if (statistics == NULL)
2063 if (cYAML_create_number(statistics, "send_count",
2064 stats->iel_send_count)
2068 if (cYAML_create_number(statistics, "recv_count",
2069 stats->iel_recv_count)
2073 if (cYAML_create_number(statistics, "drop_count",
2074 stats->iel_drop_count)
2079 goto continue_without_msg_stats;
2081 LIBCFS_IOC_INIT_V2(msg_stats, im_hdr);
2082 msg_stats.im_hdr.ioc_len = sizeof(msg_stats);
2083 msg_stats.im_idx = i;
2085 rc = l_ioctl(LNET_DEV_ID,
2086 IOC_LIBCFS_GET_LOCAL_NI_MSG_STATS,
2090 goto continue_without_msg_stats;
2093 for (k = 0; k < 3; k++) {
2094 struct lnet_ioctl_comm_count *counts;
2095 struct cYAML *msg_statistics = NULL;
2097 msg_statistics = cYAML_create_object(item,
2098 (char *)gmsg_stat_names[k]);
2099 if (msg_statistics == NULL)
2102 counts = get_counts(&msg_stats, k);
2106 if (!add_msg_stats_to_yaml_blk(msg_statistics,
2111 continue_without_msg_stats:
2112 tunables = cYAML_create_object(item, "tunables");
2116 rc = lustre_net_show_tunables(tunables, &lnd->lt_cmn);
2117 if (rc != LUSTRE_CFG_RC_NO_ERR)
2120 rc = lustre_ni_show_tunables(tunables, LNET_NETTYP(rc_net),
2122 if (rc != LUSTRE_CFG_RC_NO_ERR &&
2123 rc != LUSTRE_CFG_RC_NO_MATCH)
2126 if (rc != LUSTRE_CFG_RC_NO_MATCH) {
2127 tunables = cYAML_create_object(item,
2129 if (tunables == NULL)
2134 cYAML_create_number(item, "dev cpt",
2135 ni_data->lic_dev_cpt) == NULL)
2139 cYAML_create_number(item, "tcp bonding",
2140 ni_data->lic_tcp_bonding)
2144 /* out put the CPTs in the format: "[x,x,x,...]" */
2145 limit = str_buf + str_buf_len - 3;
2146 pos += snprintf(pos, limit - pos, "\"[");
2147 for (j = 0 ; ni_data->lic_ncpts >= 1 &&
2148 j < ni_data->lic_ncpts &&
2150 pos += snprintf(pos, limit - pos,
2151 "%d", ni_data->lic_cpts[j]);
2152 if ((j + 1) < ni_data->lic_ncpts)
2153 pos += snprintf(pos, limit - pos, ",");
2155 pos += snprintf(pos, 3, "]\"");
2157 if (ni_data->lic_ncpts >= 1 &&
2158 cYAML_create_string(item, "CPT",
2164 /* Print out the net information only if show_rc is not provided */
2165 if (show_rc == NULL)
2166 cYAML_print_tree(root);
2168 if (l_errno != ENOENT) {
2171 "\"cannot get networks: %s\"",
2176 rc = LUSTRE_CFG_RC_NO_ERR;
2178 snprintf(err_str, sizeof(err_str), "\"success\"");
2180 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2181 cYAML_free_tree(root);
2182 } else if (show_rc != NULL && *show_rc != NULL) {
2183 struct cYAML *show_node;
2184 /* find the net node, if one doesn't exist
2185 * then insert one. Otherwise add to the one there
2187 show_node = cYAML_get_object_item(*show_rc, "net");
2188 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2189 cYAML_insert_child(show_node, first_seq);
2192 } else if (show_node == NULL) {
2193 cYAML_insert_sibling((*show_rc)->cy_child,
2197 cYAML_free_tree(root);
2203 cYAML_build_error(rc, seq_no, SHOW_CMD, "net", err_str, err_rc);
2208 int lustre_lnet_enable_routing(int enable, int seq_no, struct cYAML **err_rc)
2210 struct lnet_ioctl_config_data data;
2211 int rc = LUSTRE_CFG_RC_NO_ERR;
2212 char err_str[LNET_MAX_STR_LEN];
2214 snprintf(err_str, sizeof(err_str), "\"success\"");
2216 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2217 data.cfg_config_u.cfg_buffers.buf_enable = (enable) ? 1 : 0;
2219 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIG_RTR, &data);
2224 "\"cannot %s routing %s\"",
2225 (enable) ? "enable" : "disable", strerror(errno));
2230 cYAML_build_error(rc, seq_no,
2231 (enable) ? ADD_CMD : DEL_CMD,
2232 "routing", err_str, err_rc);
2237 int ioctl_set_value(__u32 val, int ioc, char *name,
2238 int seq_no, struct cYAML **err_rc)
2240 struct lnet_ioctl_set_value data;
2241 int rc = LUSTRE_CFG_RC_NO_ERR;
2242 char err_str[LNET_MAX_STR_LEN];
2244 snprintf(err_str, sizeof(err_str), "\"success\"");
2246 LIBCFS_IOC_INIT_V2(data, sv_hdr);
2247 data.sv_value = val;
2249 rc = l_ioctl(LNET_DEV_ID, ioc , &data);
2254 "\"cannot configure %s to %d: %s\"", name,
2255 val, strerror(errno));
2258 cYAML_build_error(rc, seq_no, ADD_CMD, name, err_str, err_rc);
2263 int lustre_lnet_config_max_intf(int max, int seq_no, struct cYAML **err_rc)
2265 int rc = LUSTRE_CFG_RC_NO_ERR;
2266 char err_str[LNET_MAX_STR_LEN];
2267 char val[LNET_MAX_STR_LEN];
2269 snprintf(err_str, sizeof(err_str), "\"success\"");
2271 snprintf(val, sizeof(val), "%d", max);
2273 rc = write_sysfs_file(modparam_path, "lnet_interfaces_max", val,
2274 1, strlen(val) + 1);
2276 snprintf(err_str, sizeof(err_str),
2277 "\"cannot configure max interfaces: %s\"",
2280 cYAML_build_error(rc, seq_no, ADD_CMD, "max_interfaces", err_str, err_rc);
2285 int lustre_lnet_config_discovery(int enable, int seq_no, struct cYAML **err_rc)
2287 int rc = LUSTRE_CFG_RC_NO_ERR;
2288 char err_str[LNET_MAX_STR_LEN];
2289 char val[LNET_MAX_STR_LEN];
2291 snprintf(err_str, sizeof(err_str), "\"success\"");
2293 snprintf(val, sizeof(val), "%u", (enable) ? 0 : 1);
2295 rc = write_sysfs_file(modparam_path, "lnet_peer_discovery_disabled", val,
2296 1, strlen(val) + 1);
2298 snprintf(err_str, sizeof(err_str),
2299 "\"cannot configure discovery: %s\"",
2302 cYAML_build_error(rc, seq_no, ADD_CMD, "discovery", err_str, err_rc);
2308 int lustre_lnet_config_numa_range(int range, int seq_no, struct cYAML **err_rc)
2310 return ioctl_set_value(range, IOC_LIBCFS_SET_NUMA_RANGE,
2311 "numa_range", seq_no, err_rc);
2314 int lustre_lnet_config_buffers(int tiny, int small, int large, int seq_no,
2315 struct cYAML **err_rc)
2317 struct lnet_ioctl_config_data data;
2318 int rc = LUSTRE_CFG_RC_NO_ERR;
2319 char err_str[LNET_MAX_STR_LEN];
2321 snprintf(err_str, sizeof(err_str), "\"success\"");
2323 /* -1 indicates to ignore changes to this field */
2324 if (tiny < -1 || small < -1 || large < -1) {
2327 "\"tiny, small and large must be >= 0\"");
2328 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
2332 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2333 data.cfg_config_u.cfg_buffers.buf_tiny = tiny;
2334 data.cfg_config_u.cfg_buffers.buf_small = small;
2335 data.cfg_config_u.cfg_buffers.buf_large = large;
2337 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_BUF, &data);
2342 "\"cannot configure buffers: %s\"", strerror(errno));
2347 cYAML_build_error(rc, seq_no, ADD_CMD, "buf", err_str, err_rc);
2352 int lustre_lnet_show_routing(int seq_no, struct cYAML **show_rc,
2353 struct cYAML **err_rc, bool backup)
2355 struct lnet_ioctl_config_data *data;
2356 struct lnet_ioctl_pool_cfg *pool_cfg = NULL;
2357 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2360 char *pools[LNET_NRBPOOLS] = {"tiny", "small", "large"};
2361 int buf_count[LNET_NRBPOOLS] = {0};
2362 struct cYAML *root = NULL, *pools_node = NULL,
2363 *type_node = NULL, *item = NULL, *cpt = NULL,
2364 *first_seq = NULL, *buffers = NULL;
2366 char err_str[LNET_MAX_STR_LEN];
2367 char node_name[LNET_MAX_STR_LEN];
2370 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2372 buf = calloc(1, sizeof(*data) + sizeof(*pool_cfg));
2376 data = (struct lnet_ioctl_config_data *)buf;
2378 root = cYAML_create_object(NULL, NULL);
2383 pools_node = cYAML_create_object(root, "routing");
2385 pools_node = cYAML_create_seq(root, "routing");
2386 if (pools_node == NULL)
2390 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
2391 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
2392 sizeof(struct lnet_ioctl_pool_cfg);
2393 data->cfg_count = i;
2395 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_BUF, data);
2403 pool_cfg = (struct lnet_ioctl_pool_cfg *)data->cfg_bulk;
2406 goto calculate_buffers;
2408 snprintf(node_name, sizeof(node_name), "cpt[%d]", i);
2409 item = cYAML_create_seq_item(pools_node);
2413 if (first_seq == NULL)
2416 cpt = cYAML_create_object(item, node_name);
2421 /* create the tree and print */
2422 for (j = 0; j < LNET_NRBPOOLS; j++) {
2424 type_node = cYAML_create_object(cpt, pools[j]);
2425 if (type_node == NULL)
2429 cYAML_create_number(type_node, "npages",
2430 pool_cfg->pl_pools[j].pl_npages)
2434 cYAML_create_number(type_node, "nbuffers",
2435 pool_cfg->pl_pools[j].
2436 pl_nbuffers) == NULL)
2439 cYAML_create_number(type_node, "credits",
2440 pool_cfg->pl_pools[j].
2441 pl_credits) == NULL)
2444 cYAML_create_number(type_node, "mincredits",
2445 pool_cfg->pl_pools[j].
2446 pl_mincredits) == NULL)
2448 /* keep track of the total count for each of the
2449 * tiny, small and large buffers */
2450 buf_count[j] += pool_cfg->pl_pools[j].pl_nbuffers;
2454 if (pool_cfg != NULL) {
2456 if (cYAML_create_number(pools_node, "enable",
2457 pool_cfg->pl_routing) ==
2461 goto add_buffer_section;
2464 item = cYAML_create_seq_item(pools_node);
2468 if (cYAML_create_number(item, "enable", pool_cfg->pl_routing) ==
2474 /* create a buffers entry in the show. This is necessary so that
2475 * if the YAML output is used to configure a node, the buffer
2476 * configuration takes hold */
2477 buffers = cYAML_create_object(root, "buffers");
2478 if (buffers == NULL)
2481 for (i = 0; i < LNET_NRBPOOLS; i++) {
2482 if (cYAML_create_number(buffers, pools[i], buf_count[i]) == NULL)
2486 if (show_rc == NULL)
2487 cYAML_print_tree(root);
2489 if (l_errno != ENOENT) {
2492 "\"cannot get routing information: %s\"",
2497 rc = LUSTRE_CFG_RC_NO_ERR;
2499 snprintf(err_str, sizeof(err_str), "\"success\"");
2500 rc = LUSTRE_CFG_RC_NO_ERR;
2504 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2505 cYAML_free_tree(root);
2506 } else if (show_rc != NULL && *show_rc != NULL) {
2507 struct cYAML *routing_node;
2508 /* there should exist only one routing block and one
2509 * buffers block. If there already exists a previous one
2510 * then don't add another */
2511 routing_node = cYAML_get_object_item(*show_rc, "routing");
2512 if (routing_node == NULL) {
2513 cYAML_insert_sibling((*show_rc)->cy_child,
2517 cYAML_free_tree(root);
2523 cYAML_build_error(rc, seq_no, SHOW_CMD, "routing", err_str, err_rc);
2528 int lustre_lnet_show_peer(char *knid, int detail, int seq_no,
2529 struct cYAML **show_rc, struct cYAML **err_rc,
2533 * TODO: This function is changing in a future patch to accommodate
2534 * PEER_LIST and proper filtering on any nid of the peer
2536 struct lnet_ioctl_peer_cfg peer_info;
2537 struct lnet_peer_ni_credit_info *lpni_cri;
2538 struct lnet_ioctl_element_stats *lpni_stats;
2539 struct lnet_ioctl_element_msg_stats *msg_stats;
2541 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2546 struct cYAML *root = NULL, *peer = NULL, *peer_ni = NULL,
2547 *first_seq = NULL, *peer_root = NULL, *tmp = NULL,
2548 *msg_statistics = NULL, *statistics = NULL;
2549 char err_str[LNET_MAX_STR_LEN];
2550 struct lnet_process_id *list = NULL;
2554 snprintf(err_str, sizeof(err_str),
2555 "\"out of memory\"");
2557 /* create struct cYAML root object */
2558 root = cYAML_create_object(NULL, NULL);
2562 peer_root = cYAML_create_seq(root, "peer");
2563 if (peer_root == NULL)
2567 size = count * sizeof(struct lnet_process_id);
2568 list = malloc(size);
2574 list[0].nid = libcfs_str2nid(knid);
2578 memset(&peer_info, 0, sizeof(peer_info));
2579 LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2580 peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2581 peer_info.prcfg_size = size;
2582 peer_info.prcfg_bulk = list;
2585 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_LIST,
2587 count = peer_info.prcfg_count;
2591 if (l_errno != E2BIG) {
2594 "\"cannot get peer list: %s\"",
2600 size = peer_info.prcfg_size;
2601 list = malloc(size);
2610 data = malloc(size);
2615 for (i = 0; i < count; i++) {
2617 memset(&peer_info, 0, sizeof(peer_info));
2618 LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2619 peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2620 peer_info.prcfg_prim_nid = list[i].nid;
2621 peer_info.prcfg_size = size;
2622 peer_info.prcfg_bulk = data;
2625 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_NI,
2630 if (l_errno != E2BIG) {
2633 "\"cannot get peer information: %s\"",
2639 size = peer_info.prcfg_size;
2640 data = malloc(size);
2647 peer = cYAML_create_seq_item(peer_root);
2651 if (first_seq == NULL)
2654 lnet_nid_t pnid = peer_info.prcfg_prim_nid;
2655 if (cYAML_create_string(peer, "primary nid",
2656 libcfs_nid2str(pnid))
2659 if (cYAML_create_string(peer, "Multi-Rail",
2660 peer_info.prcfg_mr ? "True" : "False")
2664 * print out the state of the peer only if details are
2669 cYAML_create_number(peer, "peer state",
2670 peer_info.prcfg_state)
2675 tmp = cYAML_create_seq(peer, "peer ni");
2680 for (j = 0; j < peer_info.prcfg_count; j++) {
2682 lpni_cri = (void*)nidp + sizeof(nidp);
2683 lpni_stats = (void *)lpni_cri + sizeof(*lpni_cri);
2684 msg_stats = (void *)lpni_stats + sizeof(*lpni_stats);
2685 lpni_data = (void *)msg_stats + sizeof(*msg_stats);
2687 peer_ni = cYAML_create_seq_item(tmp);
2688 if (peer_ni == NULL)
2691 if (cYAML_create_string(peer_ni, "nid",
2692 libcfs_nid2str(*nidp))
2699 if (cYAML_create_string(peer_ni, "state",
2700 lpni_cri->cr_aliveness)
2707 if (cYAML_create_number(peer_ni, "max_ni_tx_credits",
2708 lpni_cri->cr_ni_peer_tx_credits)
2712 if (cYAML_create_number(peer_ni, "available_tx_credits",
2713 lpni_cri->cr_peer_tx_credits)
2717 if (cYAML_create_number(peer_ni, "min_tx_credits",
2718 lpni_cri->cr_peer_min_tx_credits)
2722 if (cYAML_create_number(peer_ni, "tx_q_num_of_buf",
2723 lpni_cri->cr_peer_tx_qnob)
2727 if (cYAML_create_number(peer_ni, "available_rtr_credits",
2728 lpni_cri->cr_peer_rtr_credits)
2732 if (cYAML_create_number(peer_ni, "min_rtr_credits",
2733 lpni_cri->cr_peer_min_rtr_credits)
2737 if (cYAML_create_number(peer_ni, "refcount",
2738 lpni_cri->cr_refcount) == NULL)
2741 statistics = cYAML_create_object(peer_ni, "statistics");
2742 if (statistics == NULL)
2745 if (cYAML_create_number(statistics, "send_count",
2746 lpni_stats->iel_send_count)
2750 if (cYAML_create_number(statistics, "recv_count",
2751 lpni_stats->iel_recv_count)
2755 if (cYAML_create_number(statistics, "drop_count",
2756 lpni_stats->iel_drop_count)
2763 for (k = 0; k < 3; k++) {
2764 struct lnet_ioctl_comm_count *counts;
2766 msg_statistics = cYAML_create_object(peer_ni,
2767 (char *) gmsg_stat_names[k]);
2768 if (msg_statistics == NULL)
2771 counts = get_counts(msg_stats, k);
2775 if (!add_msg_stats_to_yaml_blk(msg_statistics,
2783 /* print output iff show_rc is not provided */
2784 if (show_rc == NULL)
2785 cYAML_print_tree(root);
2787 snprintf(err_str, sizeof(err_str), "\"success\"");
2788 rc = LUSTRE_CFG_RC_NO_ERR;
2793 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2794 cYAML_free_tree(root);
2795 } else if (show_rc != NULL && *show_rc != NULL) {
2796 struct cYAML *show_node;
2797 /* find the peer node, if one doesn't exist then
2798 * insert one. Otherwise add to the one there
2800 show_node = cYAML_get_object_item(*show_rc,
2802 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2803 cYAML_insert_child(show_node, first_seq);
2806 } else if (show_node == NULL) {
2807 cYAML_insert_sibling((*show_rc)->cy_child,
2811 cYAML_free_tree(root);
2817 cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
2823 int lustre_lnet_list_peer(int seq_no,
2824 struct cYAML **show_rc, struct cYAML **err_rc)
2826 struct lnet_ioctl_peer_cfg peer_info;
2827 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2832 struct cYAML *root = NULL, *list_root = NULL, *first_seq = NULL;
2833 char err_str[LNET_MAX_STR_LEN];
2834 struct lnet_process_id *list = NULL;
2836 snprintf(err_str, sizeof(err_str),
2837 "\"out of memory\"");
2839 memset(&peer_info, 0, sizeof(peer_info));
2841 /* create struct cYAML root object */
2842 root = cYAML_create_object(NULL, NULL);
2846 list_root = cYAML_create_seq(root, "peer list");
2847 if (list_root == NULL)
2851 size = count * sizeof(struct lnet_process_id);
2852 list = malloc(size);
2858 LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2859 peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2860 peer_info.prcfg_size = size;
2861 peer_info.prcfg_bulk = list;
2864 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_LIST, &peer_info);
2865 count = peer_info.prcfg_count;
2869 if (l_errno != E2BIG) {
2872 "\"cannot get peer list: %s\"",
2878 size = peer_info.prcfg_size;
2879 list = malloc(size);
2886 /* count is now the actual number of ids in the list. */
2887 for (i = 0; i < count; i++) {
2888 if (cYAML_create_string(list_root, "nid",
2889 libcfs_nid2str(list[i].nid))
2894 /* print output iff show_rc is not provided */
2895 if (show_rc == NULL)
2896 cYAML_print_tree(root);
2898 snprintf(err_str, sizeof(err_str), "\"success\"");
2899 rc = LUSTRE_CFG_RC_NO_ERR;
2904 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2905 cYAML_free_tree(root);
2906 } else if (show_rc != NULL && *show_rc != NULL) {
2907 struct cYAML *show_node;
2908 /* find the peer node, if one doesn't exist then
2909 * insert one. Otherwise add to the one there
2911 show_node = cYAML_get_object_item(*show_rc,
2913 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2914 cYAML_insert_child(show_node, first_seq);
2917 } else if (show_node == NULL) {
2918 cYAML_insert_sibling((*show_rc)->cy_child,
2922 cYAML_free_tree(root);
2928 cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
2934 static void add_to_global(struct cYAML *show_rc, struct cYAML *node,
2937 struct cYAML *show_node;
2939 show_node = cYAML_get_object_item(show_rc, "global");
2940 if (show_node != NULL)
2941 cYAML_insert_sibling(show_node->cy_child,
2944 cYAML_insert_sibling(show_rc->cy_child,
2949 static int build_global_yaml_entry(char *err_str, int err_len, int seq_no,
2950 char *name, __u32 value,
2951 struct cYAML **show_rc,
2952 struct cYAML **err_rc, int err)
2954 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2955 struct cYAML *root = NULL, *global = NULL;
2962 root = cYAML_create_object(NULL, NULL);
2966 global = cYAML_create_object(root, "global");
2970 if (cYAML_create_number(global, name,
2974 if (show_rc == NULL)
2975 cYAML_print_tree(root);
2977 snprintf(err_str, err_len, "\"success\"");
2979 rc = LUSTRE_CFG_RC_NO_ERR;
2982 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2983 cYAML_free_tree(root);
2984 } else if (show_rc != NULL && *show_rc != NULL) {
2985 add_to_global(*show_rc, global, root);
2990 cYAML_build_error(rc, seq_no, SHOW_CMD, "global", err_str, err_rc);
2995 static int ioctl_show_global_values(int ioc, int seq_no, char *name,
2996 struct cYAML **show_rc,
2997 struct cYAML **err_rc)
2999 struct lnet_ioctl_set_value data;
3002 char err_str[LNET_MAX_STR_LEN];
3004 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3006 LIBCFS_IOC_INIT_V2(data, sv_hdr);
3008 rc = l_ioctl(LNET_DEV_ID, ioc, &data);
3013 "\"cannot get %s: %s\"",
3014 name, strerror(l_errno));
3017 return build_global_yaml_entry(err_str, sizeof(err_str), seq_no, name,
3018 data.sv_value, show_rc, err_rc, l_errno);
3021 int lustre_lnet_show_max_intf(int seq_no, struct cYAML **show_rc,
3022 struct cYAML **err_rc)
3024 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3025 char val[LNET_MAX_STR_LEN];
3026 int max_intf = -1, l_errno = 0;
3027 char err_str[LNET_MAX_STR_LEN];
3029 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3031 rc = read_sysfs_file(modparam_path, "lnet_interfaces_max", val,
3035 snprintf(err_str, sizeof(err_str),
3036 "\"cannot get max interfaces: %d\"", rc);
3038 max_intf = atoi(val);
3041 return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3042 "max_intf", max_intf, show_rc,
3046 int lustre_lnet_show_discovery(int seq_no, struct cYAML **show_rc,
3047 struct cYAML **err_rc)
3049 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3050 char val[LNET_MAX_STR_LEN];
3051 int discovery = -1, l_errno = 0;
3052 char err_str[LNET_MAX_STR_LEN];
3054 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3056 rc = read_sysfs_file(modparam_path, "lnet_peer_discovery_disabled", val,
3060 snprintf(err_str, sizeof(err_str),
3061 "\"cannot get discovery setting: %d\"", rc);
3064 * The kernel stores a discovery disabled value. User space
3065 * shows whether discovery is enabled. So the value must be
3068 discovery = !atoi(val);
3071 return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3072 "discovery", discovery, show_rc,
3076 int lustre_lnet_show_numa_range(int seq_no, struct cYAML **show_rc,
3077 struct cYAML **err_rc)
3079 return ioctl_show_global_values(IOC_LIBCFS_GET_NUMA_RANGE, seq_no,
3080 "numa_range", show_rc, err_rc);
3083 int lustre_lnet_show_stats(int seq_no, struct cYAML **show_rc,
3084 struct cYAML **err_rc)
3086 struct lnet_ioctl_lnet_stats data;
3089 char err_str[LNET_MAX_STR_LEN];
3090 struct cYAML *root = NULL, *stats = NULL;
3092 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3094 LIBCFS_IOC_INIT_V2(data, st_hdr);
3096 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LNET_STATS, &data);
3101 "\"cannot get lnet statistics: %s\"",
3107 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3109 root = cYAML_create_object(NULL, NULL);
3113 stats = cYAML_create_object(root, "statistics");
3117 if (cYAML_create_number(stats, "msgs_alloc",
3118 data.st_cntrs.msgs_alloc) == NULL)
3121 if (cYAML_create_number(stats, "msgs_max",
3122 data.st_cntrs.msgs_max) == NULL)
3125 if (cYAML_create_number(stats, "errors",
3126 data.st_cntrs.errors) == NULL)
3129 if (cYAML_create_number(stats, "send_count",
3130 data.st_cntrs.send_count) == NULL)
3133 if (cYAML_create_number(stats, "recv_count",
3134 data.st_cntrs.recv_count) == NULL)
3137 if (cYAML_create_number(stats, "route_count",
3138 data.st_cntrs.route_count) == NULL)
3141 if (cYAML_create_number(stats, "drop_count",
3142 data.st_cntrs.drop_count) == NULL)
3145 if (cYAML_create_number(stats, "send_length",
3146 data.st_cntrs.send_length) == NULL)
3149 if (cYAML_create_number(stats, "recv_length",
3150 data.st_cntrs.recv_length) == NULL)
3153 if (cYAML_create_number(stats, "route_length",
3154 data.st_cntrs.route_length) == NULL)
3157 if (cYAML_create_number(stats, "drop_length",
3158 data.st_cntrs.drop_length) == NULL)
3161 if (show_rc == NULL)
3162 cYAML_print_tree(root);
3164 snprintf(err_str, sizeof(err_str), "\"success\"");
3165 rc = LUSTRE_CFG_RC_NO_ERR;
3167 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
3168 cYAML_free_tree(root);
3169 } else if (show_rc != NULL && *show_rc != NULL) {
3170 cYAML_insert_sibling((*show_rc)->cy_child,
3177 cYAML_build_error(rc, seq_no, SHOW_CMD, "statistics", err_str, err_rc);
3182 typedef int (*cmd_handler_t)(struct cYAML *tree,
3183 struct cYAML **show_rc,
3184 struct cYAML **err_rc);
3186 static int handle_yaml_config_route(struct cYAML *tree, struct cYAML **show_rc,
3187 struct cYAML **err_rc)
3189 struct cYAML *net, *gw, *hop, *prio, *seq_no;
3191 net = cYAML_get_object_item(tree, "net");
3192 gw = cYAML_get_object_item(tree, "gateway");
3193 hop = cYAML_get_object_item(tree, "hop");
3194 prio = cYAML_get_object_item(tree, "priority");
3195 seq_no = cYAML_get_object_item(tree, "seq_no");
3197 return lustre_lnet_config_route((net) ? net->cy_valuestring : NULL,
3198 (gw) ? gw->cy_valuestring : NULL,
3199 (hop) ? hop->cy_valueint : -1,
3200 (prio) ? prio->cy_valueint : -1,
3201 (seq_no) ? seq_no->cy_valueint : -1,
3205 static void yaml_free_string_array(char **array, int num)
3208 char **sub_array = array;
3210 for (i = 0; i < num; i++) {
3211 if (*sub_array != NULL)
3221 * 0: <intf_name>['['<expr>']']
3222 * 1: <intf_name>['['<expr>']']
3224 static int yaml_copy_intf_info(struct cYAML *intf_tree,
3225 struct lnet_dlc_network_descr *nw_descr)
3227 struct cYAML *child = NULL;
3228 int intf_num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
3229 struct lnet_dlc_intf_descr *intf_descr, *tmp;
3231 if (intf_tree == NULL || nw_descr == NULL)
3232 return LUSTRE_CFG_RC_BAD_PARAM;
3234 /* now grab all the interfaces and their cpts */
3235 child = intf_tree->cy_child;
3236 while (child != NULL) {
3237 if (child->cy_valuestring == NULL) {
3238 child = child->cy_next;
3242 if (strlen(child->cy_valuestring) >= LNET_MAX_STR_LEN)
3245 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist,
3246 child->cy_valuestring,
3247 strlen(child->cy_valuestring));
3248 if (rc != LUSTRE_CFG_RC_NO_ERR)
3252 child = child->cy_next;
3256 return LUSTRE_CFG_RC_MISSING_PARAM;
3261 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
3263 list_del(&intf_descr->intf_on_network);
3264 free_intf_descr(intf_descr);
3271 yaml_extract_cmn_tunables(struct cYAML *tree,
3272 struct lnet_ioctl_config_lnd_cmn_tunables *tunables,
3273 struct cfs_expr_list **global_cpts)
3275 struct cYAML *tun, *item, *smp;
3278 tun = cYAML_get_object_item(tree, "tunables");
3280 item = cYAML_get_object_item(tun, "peer_timeout");
3282 tunables->lct_peer_timeout = item->cy_valueint;
3283 item = cYAML_get_object_item(tun, "peer_credits");
3285 tunables->lct_peer_tx_credits = item->cy_valueint;
3286 item = cYAML_get_object_item(tun, "peer_buffer_credits");
3288 tunables->lct_peer_rtr_credits = item->cy_valueint;
3289 item = cYAML_get_object_item(tun, "credits");
3291 tunables->lct_max_tx_credits = item->cy_valueint;
3292 smp = cYAML_get_object_item(tun, "CPT");
3294 rc = cfs_expr_list_parse(smp->cy_valuestring,
3295 strlen(smp->cy_valuestring),
3296 0, UINT_MAX, global_cpts);
3298 *global_cpts = NULL;
3308 yaml_extract_tunables(struct cYAML *tree,
3309 struct lnet_ioctl_config_lnd_tunables *tunables,
3310 struct cfs_expr_list **global_cpts,
3315 rc = yaml_extract_cmn_tunables(tree, &tunables->lt_cmn,
3321 lustre_yaml_extract_lnd_tunables(tree, net_type,
3329 * - net type: <net>[<NUM>]
3331 * - nid: <ip>@<net>[<NUM>]
3334 * 0: <intf_name>['['<expr>']']
3335 * 1: <intf_name>['['<expr>']']
3337 * peer_timeout: <NUM>
3338 * peer_credits: <NUM>
3339 * peer_buffer_credits: <NUM>
3342 * peercredits_hiw: <NUM>
3343 * map_on_demand: <NUM>
3344 * concurrent_sends: <NUM>
3345 * fmr_pool_size: <NUM>
3346 * fmr_flush_trigger: <NUM>
3349 * At least one interface is required. If no interfaces are provided the
3350 * network interface can not be configured.
3352 static int handle_yaml_config_ni(struct cYAML *tree, struct cYAML **show_rc,
3353 struct cYAML **err_rc)
3355 struct cYAML *net, *intf, *seq_no, *ip2net = NULL, *local_nis = NULL,
3357 int num_entries = 0, rc;
3358 struct lnet_dlc_network_descr nw_descr;
3359 struct cfs_expr_list *global_cpts = NULL;
3360 struct lnet_ioctl_config_lnd_tunables tunables;
3363 memset(&tunables, 0, sizeof(tunables));
3365 INIT_LIST_HEAD(&nw_descr.network_on_rule);
3366 INIT_LIST_HEAD(&nw_descr.nw_intflist);
3368 ip2net = cYAML_get_object_item(tree, "ip2net");
3369 net = cYAML_get_object_item(tree, "net type");
3371 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
3373 nw_descr.nw_id = LOLND;
3376 * if neither net nor ip2nets are present, then we can not
3377 * configure the network.
3379 if (!net && !ip2net)
3380 return LUSTRE_CFG_RC_MISSING_PARAM;
3382 local_nis = cYAML_get_object_item(tree, "local NI(s)");
3383 if (local_nis == NULL)
3384 return LUSTRE_CFG_RC_MISSING_PARAM;
3386 if (!cYAML_is_sequence(local_nis))
3387 return LUSTRE_CFG_RC_BAD_PARAM;
3389 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
3390 intf = cYAML_get_object_item(item, "interfaces");
3393 num_entries = yaml_copy_intf_info(intf, &nw_descr);
3394 if (num_entries <= 0) {
3395 cYAML_build_error(num_entries, -1, "ni", "add",
3396 "bad interface list",
3398 return LUSTRE_CFG_RC_BAD_PARAM;
3402 found = yaml_extract_tunables(tree, &tunables, &global_cpts,
3403 LNET_NETTYP(nw_descr.nw_id));
3404 seq_no = cYAML_get_object_item(tree, "seq_no");
3406 rc = lustre_lnet_config_ni(&nw_descr,
3408 (ip2net) ? ip2net->cy_valuestring : NULL,
3409 (found) ? &tunables: NULL,
3410 (seq_no) ? seq_no->cy_valueint : -1,
3413 if (global_cpts != NULL)
3414 cfs_expr_list_free(global_cpts);
3421 * - net-spec: <tcp|o2ib|gni>[NUM]
3423 * 0: <intf name>['['<expr>']']
3424 * 1: <intf name>['['<expr>']']
3426 * 0: <expr.expr.expr.expr>
3427 * 1: <expr.expr.expr.expr>
3429 static int handle_yaml_config_ip2nets(struct cYAML *tree,
3430 struct cYAML **show_rc,
3431 struct cYAML **err_rc)
3433 struct cYAML *net, *ip_range, *item = NULL, *intf = NULL,
3435 struct lustre_lnet_ip2nets ip2nets;
3436 struct lustre_lnet_ip_range_descr *ip_range_descr = NULL,
3438 int rc = LUSTRE_CFG_RC_NO_ERR;
3439 struct cfs_expr_list *global_cpts = NULL;
3440 struct cfs_expr_list *el, *el_tmp;
3441 struct lnet_ioctl_config_lnd_tunables tunables;
3442 struct lnet_dlc_intf_descr *intf_descr, *intf_tmp;
3445 memset(&tunables, 0, sizeof(tunables));
3447 /* initialize all lists */
3448 INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
3449 INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
3450 INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
3452 net = cYAML_get_object_item(tree, "net-spec");
3454 return LUSTRE_CFG_RC_BAD_PARAM;
3456 if (net != NULL && net->cy_valuestring == NULL)
3457 return LUSTRE_CFG_RC_BAD_PARAM;
3459 /* assign the network id */
3460 ip2nets.ip2nets_net.nw_id = libcfs_str2net(net->cy_valuestring);
3461 if (ip2nets.ip2nets_net.nw_id == LNET_NID_ANY)
3462 return LUSTRE_CFG_RC_BAD_PARAM;
3464 seq_no = cYAML_get_object_item(tree, "seq_no");
3466 intf = cYAML_get_object_item(tree, "interfaces");
3468 rc = yaml_copy_intf_info(intf, &ip2nets.ip2nets_net);
3470 return LUSTRE_CFG_RC_BAD_PARAM;
3473 ip_range = cYAML_get_object_item(tree, "ip-range");
3474 if (ip_range != NULL) {
3475 item = ip_range->cy_child;
3476 while (item != NULL) {
3477 if (item->cy_valuestring == NULL) {
3478 item = item->cy_next;
3482 rc = lustre_lnet_add_ip_range(&ip2nets.ip2nets_ip_ranges,
3483 item->cy_valuestring);
3485 if (rc != LUSTRE_CFG_RC_NO_ERR)
3488 item = item->cy_next;
3492 found = yaml_extract_tunables(tree, &tunables, &global_cpts,
3493 LNET_NETTYP(ip2nets.ip2nets_net.nw_id));
3495 rc = lustre_lnet_config_ip2nets(&ip2nets,
3496 (found) ? &tunables : NULL,
3498 (seq_no) ? seq_no->cy_valueint : -1,
3502 * don't stop because there was no match. Continue processing the
3503 * rest of the rules. If non-match then nothing is configured
3505 if (rc == LUSTRE_CFG_RC_NO_MATCH)
3506 rc = LUSTRE_CFG_RC_NO_ERR;
3508 list_for_each_entry_safe(intf_descr, intf_tmp,
3509 &ip2nets.ip2nets_net.nw_intflist,
3511 list_del(&intf_descr->intf_on_network);
3512 free_intf_descr(intf_descr);
3515 list_for_each_entry_safe(ip_range_descr, tmp,
3516 &ip2nets.ip2nets_ip_ranges,
3518 list_del(&ip_range_descr->ipr_entry);
3519 list_for_each_entry_safe(el, el_tmp, &ip_range_descr->ipr_expr,
3521 list_del(&el->el_link);
3522 cfs_expr_list_free(el);
3524 free(ip_range_descr);
3530 static int handle_yaml_del_ni(struct cYAML *tree, struct cYAML **show_rc,
3531 struct cYAML **err_rc)
3533 struct cYAML *net = NULL, *intf = NULL, *seq_no = NULL, *item = NULL,
3535 int num_entries, rc;
3536 struct lnet_dlc_network_descr nw_descr;
3538 INIT_LIST_HEAD(&nw_descr.network_on_rule);
3539 INIT_LIST_HEAD(&nw_descr.nw_intflist);
3541 net = cYAML_get_object_item(tree, "net type");
3543 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
3545 local_nis = cYAML_get_object_item(tree, "local NI(s)");
3546 if (local_nis == NULL)
3547 return LUSTRE_CFG_RC_MISSING_PARAM;
3549 if (!cYAML_is_sequence(local_nis))
3550 return LUSTRE_CFG_RC_BAD_PARAM;
3552 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
3553 intf = cYAML_get_object_item(item, "interfaces");
3556 num_entries = yaml_copy_intf_info(intf, &nw_descr);
3557 if (num_entries <= 0) {
3558 cYAML_build_error(num_entries, -1, "ni", "add",
3559 "bad interface list",
3561 return LUSTRE_CFG_RC_BAD_PARAM;
3565 seq_no = cYAML_get_object_item(tree, "seq_no");
3567 rc = lustre_lnet_del_ni((net) ? &nw_descr : NULL,
3568 (seq_no) ? seq_no->cy_valueint : -1,
3574 static int yaml_copy_peer_nids(struct cYAML *tree, char ***nidsppp, bool del)
3576 struct cYAML *nids_entry = NULL, *child = NULL, *entry = NULL,
3579 int num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
3581 prim_nid = cYAML_get_object_item(tree, "primary nid");
3582 if (!prim_nid || !prim_nid->cy_valuestring)
3583 return LUSTRE_CFG_RC_MISSING_PARAM;
3585 nids_entry = cYAML_get_object_item(tree, "peer ni");
3586 if (cYAML_is_sequence(nids_entry)) {
3587 while (cYAML_get_next_seq_item(nids_entry, &child)) {
3588 entry = cYAML_get_object_item(child, "nid");
3589 /* don't count an empty entry */
3590 if (!entry || !entry->cy_valuestring)
3593 if ((strcmp(entry->cy_valuestring, prim_nid->cy_valuestring)
3596 * primary nid is present in the list of
3597 * nids so that means we want to delete
3598 * the entire peer, so no need to go
3599 * further. Just delete the entire peer.
3609 return LUSTRE_CFG_RC_MISSING_PARAM;
3611 nids = calloc(sizeof(*nids) * num, 1);
3613 return LUSTRE_CFG_RC_OUT_OF_MEM;
3615 /* now grab all the nids */
3618 while (cYAML_get_next_seq_item(nids_entry, &child)) {
3619 entry = cYAML_get_object_item(child, "nid");
3620 if (!entry || !entry->cy_valuestring)
3623 nids[num] = calloc(strlen(entry->cy_valuestring) + 1, 1);
3625 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3628 strncpy(nids[num], entry->cy_valuestring,
3629 strlen(entry->cy_valuestring));
3639 yaml_free_string_array(nids, num);
3644 static int handle_yaml_config_peer(struct cYAML *tree, struct cYAML **show_rc,
3645 struct cYAML **err_rc)
3649 struct cYAML *seq_no, *prim_nid, *non_mr;
3651 num = yaml_copy_peer_nids(tree, &nids, false);
3655 seq_no = cYAML_get_object_item(tree, "seq_no");
3656 prim_nid = cYAML_get_object_item(tree, "primary nid");
3657 non_mr = cYAML_get_object_item(tree, "non_mr");
3659 rc = lustre_lnet_config_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
3661 (non_mr) ? false : true,
3662 (seq_no) ? seq_no->cy_valueint : -1,
3665 yaml_free_string_array(nids, num);
3669 static int handle_yaml_del_peer(struct cYAML *tree, struct cYAML **show_rc,
3670 struct cYAML **err_rc)
3674 struct cYAML *seq_no, *prim_nid;
3676 num = yaml_copy_peer_nids(tree, &nids, true);
3680 seq_no = cYAML_get_object_item(tree, "seq_no");
3681 prim_nid = cYAML_get_object_item(tree, "primary nid");
3683 rc = lustre_lnet_del_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
3685 (seq_no) ? seq_no->cy_valueint : -1,
3688 yaml_free_string_array(nids, num);
3692 static int handle_yaml_config_buffers(struct cYAML *tree,
3693 struct cYAML **show_rc,
3694 struct cYAML **err_rc)
3697 struct cYAML *tiny, *small, *large, *seq_no;
3699 tiny = cYAML_get_object_item(tree, "tiny");
3700 small = cYAML_get_object_item(tree, "small");
3701 large = cYAML_get_object_item(tree, "large");
3702 seq_no = cYAML_get_object_item(tree, "seq_no");
3704 rc = lustre_lnet_config_buffers((tiny) ? tiny->cy_valueint : -1,
3705 (small) ? small->cy_valueint : -1,
3706 (large) ? large->cy_valueint : -1,
3707 (seq_no) ? seq_no->cy_valueint : -1,
3713 static int handle_yaml_config_routing(struct cYAML *tree,
3714 struct cYAML **show_rc,
3715 struct cYAML **err_rc)
3717 int rc = LUSTRE_CFG_RC_NO_ERR;
3718 struct cYAML *seq_no, *enable;
3720 seq_no = cYAML_get_object_item(tree, "seq_no");
3721 enable = cYAML_get_object_item(tree, "enable");
3724 rc = lustre_lnet_enable_routing(enable->cy_valueint,
3726 seq_no->cy_valueint : -1,
3733 static int handle_yaml_del_route(struct cYAML *tree, struct cYAML **show_rc,
3734 struct cYAML **err_rc)
3738 struct cYAML *seq_no;
3740 net = cYAML_get_object_item(tree, "net");
3741 gw = cYAML_get_object_item(tree, "gateway");
3742 seq_no = cYAML_get_object_item(tree, "seq_no");
3744 return lustre_lnet_del_route((net) ? net->cy_valuestring : NULL,
3745 (gw) ? gw->cy_valuestring : NULL,
3746 (seq_no) ? seq_no->cy_valueint : -1,
3750 static int handle_yaml_del_routing(struct cYAML *tree, struct cYAML **show_rc,
3751 struct cYAML **err_rc)
3753 struct cYAML *seq_no;
3755 seq_no = cYAML_get_object_item(tree, "seq_no");
3757 return lustre_lnet_enable_routing(0, (seq_no) ?
3758 seq_no->cy_valueint : -1,
3762 static int handle_yaml_show_route(struct cYAML *tree, struct cYAML **show_rc,
3763 struct cYAML **err_rc)
3769 struct cYAML *detail;
3770 struct cYAML *seq_no;
3772 net = cYAML_get_object_item(tree, "net");
3773 gw = cYAML_get_object_item(tree, "gateway");
3774 hop = cYAML_get_object_item(tree, "hop");
3775 prio = cYAML_get_object_item(tree, "priority");
3776 detail = cYAML_get_object_item(tree, "detail");
3777 seq_no = cYAML_get_object_item(tree, "seq_no");
3779 return lustre_lnet_show_route((net) ? net->cy_valuestring : NULL,
3780 (gw) ? gw->cy_valuestring : NULL,
3781 (hop) ? hop->cy_valueint : -1,
3782 (prio) ? prio->cy_valueint : -1,
3783 (detail) ? detail->cy_valueint : 0,
3784 (seq_no) ? seq_no->cy_valueint : -1,
3785 show_rc, err_rc, false);
3788 static int handle_yaml_show_net(struct cYAML *tree, struct cYAML **show_rc,
3789 struct cYAML **err_rc)
3791 struct cYAML *net, *detail, *seq_no;
3793 net = cYAML_get_object_item(tree, "net");
3794 detail = cYAML_get_object_item(tree, "detail");
3795 seq_no = cYAML_get_object_item(tree, "seq_no");
3797 return lustre_lnet_show_net((net) ? net->cy_valuestring : NULL,
3798 (detail) ? detail->cy_valueint : 0,
3799 (seq_no) ? seq_no->cy_valueint : -1,
3800 show_rc, err_rc, false);
3803 static int handle_yaml_show_routing(struct cYAML *tree, struct cYAML **show_rc,
3804 struct cYAML **err_rc)
3806 struct cYAML *seq_no;
3808 seq_no = cYAML_get_object_item(tree, "seq_no");
3810 return lustre_lnet_show_routing((seq_no) ? seq_no->cy_valueint : -1,
3811 show_rc, err_rc, false);
3814 static int handle_yaml_show_peers(struct cYAML *tree, struct cYAML **show_rc,
3815 struct cYAML **err_rc)
3817 struct cYAML *seq_no, *nid, *detail;
3819 seq_no = cYAML_get_object_item(tree, "seq_no");
3820 detail = cYAML_get_object_item(tree, "detail");
3821 nid = cYAML_get_object_item(tree, "nid");
3823 return lustre_lnet_show_peer((nid) ? nid->cy_valuestring : NULL,
3824 (detail) ? detail->cy_valueint : 0,
3825 (seq_no) ? seq_no->cy_valueint : -1,
3826 show_rc, err_rc, false);
3829 static int handle_yaml_show_stats(struct cYAML *tree, struct cYAML **show_rc,
3830 struct cYAML **err_rc)
3832 struct cYAML *seq_no;
3834 seq_no = cYAML_get_object_item(tree, "seq_no");
3836 return lustre_lnet_show_stats((seq_no) ? seq_no->cy_valueint : -1,
3840 static int handle_yaml_config_numa(struct cYAML *tree, struct cYAML **show_rc,
3841 struct cYAML **err_rc)
3843 struct cYAML *seq_no, *range;
3845 seq_no = cYAML_get_object_item(tree, "seq_no");
3846 range = cYAML_get_object_item(tree, "range");
3848 return lustre_lnet_config_numa_range(range ? range->cy_valueint : -1,
3849 seq_no ? seq_no->cy_valueint : -1,
3853 static int handle_yaml_del_numa(struct cYAML *tree, struct cYAML **show_rc,
3854 struct cYAML **err_rc)
3856 struct cYAML *seq_no;
3858 seq_no = cYAML_get_object_item(tree, "seq_no");
3860 return lustre_lnet_config_numa_range(0, seq_no ? seq_no->cy_valueint : -1,
3864 static int handle_yaml_show_numa(struct cYAML *tree, struct cYAML **show_rc,
3865 struct cYAML **err_rc)
3867 struct cYAML *seq_no;
3869 seq_no = cYAML_get_object_item(tree, "seq_no");
3871 return lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint : -1,
3875 static int handle_yaml_config_global_settings(struct cYAML *tree,
3876 struct cYAML **show_rc,
3877 struct cYAML **err_rc)
3879 struct cYAML *max_intf, *numa, *discovery, *seq_no;
3882 seq_no = cYAML_get_object_item(tree, "seq_no");
3883 max_intf = cYAML_get_object_item(tree, "max_intf");
3885 rc = lustre_lnet_config_max_intf(max_intf->cy_valueint,
3886 seq_no ? seq_no->cy_valueint
3890 numa = cYAML_get_object_item(tree, "numa_range");
3892 rc = lustre_lnet_config_numa_range(numa->cy_valueint,
3893 seq_no ? seq_no->cy_valueint
3897 discovery = cYAML_get_object_item(tree, "discovery");
3899 rc = lustre_lnet_config_discovery(discovery->cy_valueint,
3900 seq_no ? seq_no->cy_valueint
3907 static int handle_yaml_del_global_settings(struct cYAML *tree,
3908 struct cYAML **show_rc,
3909 struct cYAML **err_rc)
3911 struct cYAML *max_intf, *numa, *discovery, *seq_no;
3914 seq_no = cYAML_get_object_item(tree, "seq_no");
3915 max_intf = cYAML_get_object_item(tree, "max_intf");
3917 rc = lustre_lnet_config_max_intf(LNET_INTERFACES_MAX_DEFAULT,
3918 seq_no ? seq_no->cy_valueint
3922 numa = cYAML_get_object_item(tree, "numa_range");
3924 rc = lustre_lnet_config_numa_range(0,
3925 seq_no ? seq_no->cy_valueint
3929 /* peer discovery is enabled by default */
3930 discovery = cYAML_get_object_item(tree, "discovery");
3932 rc = lustre_lnet_config_discovery(1,
3933 seq_no ? seq_no->cy_valueint
3940 static int handle_yaml_show_global_settings(struct cYAML *tree,
3941 struct cYAML **show_rc,
3942 struct cYAML **err_rc)
3944 struct cYAML *max_intf, *numa, *discovery, *seq_no;
3947 seq_no = cYAML_get_object_item(tree, "seq_no");
3948 max_intf = cYAML_get_object_item(tree, "max_intf");
3950 rc = lustre_lnet_show_max_intf(seq_no ? seq_no->cy_valueint
3954 numa = cYAML_get_object_item(tree, "numa_range");
3956 rc = lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint
3960 discovery = cYAML_get_object_item(tree, "discovery");
3962 rc = lustre_lnet_show_discovery(seq_no ? seq_no->cy_valueint
3969 static int handle_yaml_ping(struct cYAML *tree, struct cYAML **show_rc,
3970 struct cYAML **err_rc)
3972 struct cYAML *seq_no, *nid, *timeout;
3974 seq_no = cYAML_get_object_item(tree, "seq_no");
3975 nid = cYAML_get_object_item(tree, "primary nid");
3976 timeout = cYAML_get_object_item(tree, "timeout");
3978 return lustre_lnet_ping_nid((nid) ? nid->cy_valuestring : NULL,
3979 (timeout) ? timeout->cy_valueint : 1000,
3980 (seq_no) ? seq_no->cy_valueint : -1,
3984 static int handle_yaml_discover(struct cYAML *tree, struct cYAML **show_rc,
3985 struct cYAML **err_rc)
3987 struct cYAML *seq_no, *nid, *force;
3989 seq_no = cYAML_get_object_item(tree, "seq_no");
3990 nid = cYAML_get_object_item(tree, "primary nid");
3991 force = cYAML_get_object_item(tree, "force");
3993 return lustre_lnet_discover_nid((nid) ? nid->cy_valuestring : NULL,
3994 (force) ? force->cy_valueint : 0,
3995 (seq_no) ? seq_no->cy_valueint : -1,
3999 static int handle_yaml_no_op()
4001 return LUSTRE_CFG_RC_NO_ERR;
4004 struct lookup_cmd_hdlr_tbl {
4009 static struct lookup_cmd_hdlr_tbl lookup_config_tbl[] = {
4010 { .name = "route", .cb = handle_yaml_config_route },
4011 { .name = "net", .cb = handle_yaml_config_ni },
4012 { .name = "ip2nets", .cb = handle_yaml_config_ip2nets },
4013 { .name = "peer", .cb = handle_yaml_config_peer },
4014 { .name = "routing", .cb = handle_yaml_config_routing },
4015 { .name = "buffers", .cb = handle_yaml_config_buffers },
4016 { .name = "statistics", .cb = handle_yaml_no_op },
4017 { .name = "global", .cb = handle_yaml_config_global_settings},
4018 { .name = "numa", .cb = handle_yaml_config_numa },
4019 { .name = "ping", .cb = handle_yaml_no_op },
4020 { .name = "discover", .cb = handle_yaml_no_op },
4023 static struct lookup_cmd_hdlr_tbl lookup_del_tbl[] = {
4024 { .name = "route", .cb = handle_yaml_del_route },
4025 { .name = "net", .cb = handle_yaml_del_ni },
4026 { .name = "ip2nets", .cb = handle_yaml_no_op },
4027 { .name = "peer", .cb = handle_yaml_del_peer },
4028 { .name = "routing", .cb = handle_yaml_del_routing },
4029 { .name = "buffers", .cb = handle_yaml_no_op },
4030 { .name = "statistics", .cb = handle_yaml_no_op },
4031 { .name = "global", .cb = handle_yaml_del_global_settings},
4032 { .name = "numa", .cb = handle_yaml_del_numa },
4033 { .name = "ping", .cb = handle_yaml_no_op },
4034 { .name = "discover", .cb = handle_yaml_no_op },
4037 static struct lookup_cmd_hdlr_tbl lookup_show_tbl[] = {
4038 { .name = "route", .cb = handle_yaml_show_route },
4039 { .name = "net", .cb = handle_yaml_show_net },
4040 { .name = "peer", .cb = handle_yaml_show_peers },
4041 { .name = "ip2nets", .cb = handle_yaml_no_op },
4042 { .name = "routing", .cb = handle_yaml_show_routing },
4043 { .name = "buffers", .cb = handle_yaml_show_routing },
4044 { .name = "statistics", .cb = handle_yaml_show_stats },
4045 { .name = "global", .cb = handle_yaml_show_global_settings},
4046 { .name = "numa", .cb = handle_yaml_show_numa },
4047 { .name = "ping", .cb = handle_yaml_no_op },
4048 { .name = "discover", .cb = handle_yaml_no_op },
4051 static struct lookup_cmd_hdlr_tbl lookup_exec_tbl[] = {
4052 { .name = "route", .cb = handle_yaml_no_op },
4053 { .name = "net", .cb = handle_yaml_no_op },
4054 { .name = "peer", .cb = handle_yaml_no_op },
4055 { .name = "ip2nets", .cb = handle_yaml_no_op },
4056 { .name = "routing", .cb = handle_yaml_no_op },
4057 { .name = "buffers", .cb = handle_yaml_no_op },
4058 { .name = "statistics", .cb = handle_yaml_no_op },
4059 { .name = "global", .cb = handle_yaml_no_op },
4060 { .name = "numa", .cb = handle_yaml_no_op },
4061 { .name = "ping", .cb = handle_yaml_ping },
4062 { .name = "discover", .cb = handle_yaml_discover },
4065 static cmd_handler_t lookup_fn(char *key,
4066 struct lookup_cmd_hdlr_tbl *tbl)
4072 for (i = 0; tbl[i].name != NULL; i++) {
4073 if (strncmp(key, tbl[i].name, strlen(tbl[i].name)) == 0)
4080 static int lustre_yaml_cb_helper(char *f, struct lookup_cmd_hdlr_tbl *table,
4081 struct cYAML **show_rc, struct cYAML **err_rc)
4083 struct cYAML *tree, *item = NULL, *head, *child;
4085 char err_str[LNET_MAX_STR_LEN];
4086 int rc = LUSTRE_CFG_RC_NO_ERR, return_rc = LUSTRE_CFG_RC_NO_ERR;
4088 tree = cYAML_build_tree(f, NULL, 0, err_rc, false);
4090 return LUSTRE_CFG_RC_BAD_PARAM;
4092 child = tree->cy_child;
4093 while (child != NULL) {
4094 cb = lookup_fn(child->cy_string, table);
4096 snprintf(err_str, sizeof(err_str),
4097 "\"call back for '%s' not found\"",
4099 cYAML_build_error(LUSTRE_CFG_RC_BAD_PARAM, -1,
4100 "yaml", "helper", err_str, err_rc);
4104 if (cYAML_is_sequence(child)) {
4105 while ((head = cYAML_get_next_seq_item(child, &item))
4107 rc = cb(head, show_rc, err_rc);
4108 if (rc != LUSTRE_CFG_RC_NO_ERR)
4112 rc = cb(child, show_rc, err_rc);
4113 if (rc != LUSTRE_CFG_RC_NO_ERR)
4117 child = child->cy_next;
4121 cYAML_free_tree(tree);
4126 int lustre_yaml_config(char *f, struct cYAML **err_rc)
4128 return lustre_yaml_cb_helper(f, lookup_config_tbl,
4132 int lustre_yaml_del(char *f, struct cYAML **err_rc)
4134 return lustre_yaml_cb_helper(f, lookup_del_tbl,
4138 int lustre_yaml_show(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
4140 return lustre_yaml_cb_helper(f, lookup_show_tbl,
4144 int lustre_yaml_exec(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
4146 return lustre_yaml_cb_helper(f, lookup_exec_tbl,