4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of the
9 * License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 * Copyright (c) 2014, 2016, Intel Corporation.
24 * Amir Shehata <amir.shehata@intel.com>
29 * 1. APIs that take the actual parameters expanded. This is for other
30 * entities that would like to link against the library and call the APIs
31 * directly without having to form an intermediate representation.
32 * 2. APIs that take a YAML file and parses out the information there and
33 * calls the APIs mentioned in 1
44 #include <sys/ioctl.h>
46 #include <libcfs/util/ioctl.h>
47 #include <linux/lnet/lnetctl.h>
49 #include <sys/types.h>
53 #include "liblnetconfig.h"
56 #define CONFIG_CMD "configure"
57 #define UNCONFIG_CMD "unconfigure"
60 #define SHOW_CMD "show"
62 #define MANAGE_CMD "manage"
64 #define modparam_path "/sys/module/lnet/parameters/"
66 const char *gmsg_stat_names[] = {"sent_stats", "received_stats",
70 * lustre_lnet_ip_range_descr
71 * Describes an IP range.
72 * Each octect is an expression
74 struct lustre_lnet_ip_range_descr {
75 struct list_head ipr_entry;
76 struct list_head ipr_expr;
81 * Describes an ip2nets rule. This can be on a list of rules.
83 struct lustre_lnet_ip2nets {
84 struct lnet_dlc_network_descr ip2nets_net;
85 struct list_head ip2nets_ip_ranges;
88 int open_sysfs_file(const char *path, const char *attr, const int mode)
91 char filename[LNET_MAX_STR_LEN];
93 if (strlen(path) + strlen(attr) >= LNET_MAX_STR_LEN)
96 snprintf(filename, sizeof(filename), "%s%s",
99 fd = open(filename, mode);
104 static int read_sysfs_file(const char *path, const char *attr,
105 void *val, const size_t size, const int nelem)
108 int rc = LUSTRE_CFG_RC_GENERIC_ERR;
110 fd = open_sysfs_file(path, attr, O_RDONLY);
112 return LUSTRE_CFG_RC_NO_MATCH;
114 if (read(fd, val, size * nelem) == -1)
117 rc = LUSTRE_CFG_RC_NO_ERR;
124 static int write_sysfs_file(const char *path, const char *attr,
125 void *val, const size_t size, const int nelem)
128 int rc = LUSTRE_CFG_RC_GENERIC_ERR;
130 fd = open_sysfs_file(path, attr, O_WRONLY | O_TRUNC);
132 return LUSTRE_CFG_RC_NO_MATCH;
134 if (write(fd, val, size * nelem) == -1)
137 rc = LUSTRE_CFG_RC_NO_ERR;
146 * frees the memory allocated for an intf descriptor.
148 void free_intf_descr(struct lnet_dlc_intf_descr *intf_descr)
153 if (intf_descr->cpt_expr != NULL)
154 cfs_expr_list_free(intf_descr->cpt_expr);
159 * lustre_lnet_add_ip_range
161 * given a string of the format:
162 * <expr.expr.expr.expr> parse each expr into
163 * a lustre_lnet_ip_range_descr structure and insert on the list.
165 * This function is called from
166 * YAML on each ip-range.
167 * As a result of lnetctl command
168 * When building a NID or P2P selection rules
170 int lustre_lnet_add_ip_range(struct list_head *list, char *str_ip_range)
172 struct lustre_lnet_ip_range_descr *ip_range;
175 ip_range = calloc(1, sizeof(*ip_range));
176 if (ip_range == NULL)
177 return LUSTRE_CFG_RC_OUT_OF_MEM;
179 INIT_LIST_HEAD(&ip_range->ipr_entry);
180 INIT_LIST_HEAD(&ip_range->ipr_expr);
182 rc = cfs_ip_addr_parse(str_ip_range, strlen(str_ip_range),
183 &ip_range->ipr_expr);
185 return LUSTRE_CFG_RC_BAD_PARAM;
187 list_add_tail(&ip_range->ipr_entry, list);
189 return LUSTRE_CFG_RC_NO_ERR;
192 int lustre_lnet_add_intf_descr(struct list_head *list, char *intf, int len)
194 char *open_sq_bracket = NULL, *close_sq_bracket = NULL,
196 struct lnet_dlc_intf_descr *intf_descr = NULL;
198 char intf_string[LNET_MAX_STR_LEN];
200 if (len >= LNET_MAX_STR_LEN)
201 return LUSTRE_CFG_RC_BAD_PARAM;
203 strncpy(intf_string, intf, len);
204 intf_string[len] = '\0';
206 intf_descr = calloc(1, sizeof(*intf_descr));
207 if (intf_descr == NULL)
208 return LUSTRE_CFG_RC_OUT_OF_MEM;
210 INIT_LIST_HEAD(&intf_descr->intf_on_network);
212 intf_name = intf_string;
213 open_sq_bracket = strchr(intf_string, '[');
214 if (open_sq_bracket != NULL) {
215 close_sq_bracket = strchr(intf_string, ']');
216 if (close_sq_bracket == NULL) {
218 return LUSTRE_CFG_RC_BAD_PARAM;
220 rc = cfs_expr_list_parse(open_sq_bracket,
221 strlen(open_sq_bracket), 0, UINT_MAX,
222 &intf_descr->cpt_expr);
225 return LUSTRE_CFG_RC_BAD_PARAM;
227 strncpy(intf_descr->intf_name, intf_name,
228 open_sq_bracket - intf_name);
229 intf_descr->intf_name[open_sq_bracket - intf_name] = '\0';
231 strcpy(intf_descr->intf_name, intf_name);
232 intf_descr->cpt_expr = NULL;
235 list_add_tail(&intf_descr->intf_on_network, list);
237 return LUSTRE_CFG_RC_NO_ERR;
240 void lustre_lnet_init_nw_descr(struct lnet_dlc_network_descr *nw_descr)
242 if (nw_descr != NULL) {
243 INIT_LIST_HEAD(&nw_descr->network_on_rule);
244 INIT_LIST_HEAD(&nw_descr->nw_intflist);
248 int lustre_lnet_parse_nids(char *nids, char **array, int size,
252 char *comma = nids, *cur, *entry;
254 int i, len, start = 0, finish = 0;
256 if (nids == NULL || strlen(nids) == 0)
259 /* count the number or new nids, by counting the number of commas */
261 comma = strchr(comma, ',');
271 * if the array is not NULL allocate a large enough array to house
272 * the old and new entries
274 new_array = calloc(sizeof(char*),
275 (size > 0) ? size + num_nids : num_nids);
280 /* parse our the new nids and add them to the tail of the array */
283 start = (size > 0) ? size: 0;
284 finish = (size > 0) ? size + num_nids : num_nids;
285 for (i = start; i < finish; i++) {
286 comma = strchr(comma, ',');
289 * the length of the string to be parsed out is
290 * from cur to end of string. So it's good enough
293 len = strlen(cur) + 1;
295 /* length of the string is comma - cur */
296 len = (comma - cur) + 1;
298 entry = calloc(1, len);
300 finish = i > 0 ? i - 1: 0;
303 strncpy(entry, cur, len - 1);
305 new_array[i] = entry;
312 /* add the old entries in the array and delete the old array*/
313 for (i = 0; i < size; i++)
314 new_array[i] = array[i];
319 *out_array = new_array;
324 for (i = start; i < finish; i++)
334 * <intf>[<expr>], <intf>[<expr>],..
336 int lustre_lnet_parse_interfaces(char *intf_str,
337 struct lnet_dlc_network_descr *nw_descr)
342 char *cur = intf_str, *next = NULL;
343 char *end = intf_str + strlen(intf_str);
345 struct lnet_dlc_intf_descr *intf_descr, *tmp;
347 if (nw_descr == NULL)
348 return LUSTRE_CFG_RC_BAD_PARAM;
351 open_square = strchr(cur, '[');
352 if (open_square != NULL) {
353 close_square = strchr(cur, ']');
354 if (close_square == NULL) {
355 rc = LUSTRE_CFG_RC_BAD_PARAM;
359 comma = strchr(cur, ',');
360 if (comma != NULL && comma > close_square) {
362 len = next - close_square;
368 comma = strchr(cur, ',');
378 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist, cur, len);
379 if (rc != LUSTRE_CFG_RC_NO_ERR)
385 return LUSTRE_CFG_RC_NO_ERR;
388 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
390 list_del(&intf_descr->intf_on_network);
391 free_intf_descr(intf_descr);
397 int lustre_lnet_config_lib_init(void)
399 return register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH,
400 LNET_DEV_MAJOR, LNET_DEV_MINOR);
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 lnet_process_id_t 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(lnet_process_id_t) * 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(s): '%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(s): '%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)
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)
1075 if (cYAML_create_string(item, "state",
1076 data.cfg_config_u.cfg_route.
1078 "up" : "down") == NULL)
1083 /* print output iff show_rc is not provided */
1084 if (show_rc == NULL)
1085 cYAML_print_tree(root);
1087 if (l_errno != ENOENT) {
1090 "\"cannot get routes: %s\"",
1095 rc = LUSTRE_CFG_RC_NO_ERR;
1097 snprintf(err_str, sizeof(err_str), "\"success\"");
1099 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
1100 cYAML_free_tree(root);
1101 } else if (show_rc != NULL && *show_rc != NULL) {
1102 struct cYAML *show_node;
1103 /* find the route node, if one doesn't exist then
1104 * insert one. Otherwise add to the one there
1106 show_node = cYAML_get_object_item(*show_rc, "route");
1107 if (show_node != NULL && cYAML_is_sequence(show_node)) {
1108 cYAML_insert_child(show_node, first_seq);
1111 } else if (show_node == NULL) {
1112 cYAML_insert_sibling((*show_rc)->cy_child,
1116 cYAML_free_tree(root);
1122 cYAML_build_error(rc, seq_no, SHOW_CMD, "route", err_str, err_rc);
1127 static int socket_intf_query(int request, char *intf,
1133 if (strlen(intf) >= IFNAMSIZ || ifr == NULL)
1134 return LUSTRE_CFG_RC_BAD_PARAM;
1136 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
1138 return LUSTRE_CFG_RC_BAD_PARAM;
1140 strcpy(ifr->ifr_name, intf);
1141 rc = ioctl(sockfd, request, ifr);
1143 rc = LUSTRE_CFG_RC_BAD_PARAM;
1151 * for each interface in the array of interfaces find the IP address of
1152 * that interface, create its nid and add it to an array of NIDs.
1153 * Stop if any of the interfaces is down
1155 static int lustre_lnet_intf2nids(struct lnet_dlc_network_descr *nw,
1156 lnet_nid_t **nids, __u32 *nnids)
1158 int i = 0, count = 0, rc;
1161 struct lnet_dlc_intf_descr *intf;
1163 if (nw == NULL || nids == NULL)
1164 return LUSTRE_CFG_RC_BAD_PARAM;
1166 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network)
1169 *nids = calloc(count, sizeof(lnet_nid_t));
1171 return LUSTRE_CFG_RC_OUT_OF_MEM;
1173 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network) {
1174 memset(&ifr, 0, sizeof(ifr));
1175 rc = socket_intf_query(SIOCGIFFLAGS, intf->intf_name, &ifr);
1179 if ((ifr.ifr_flags & IFF_UP) == 0) {
1180 rc = LUSTRE_CFG_RC_BAD_PARAM;
1184 memset(&ifr, 0, sizeof(ifr));
1185 rc = socket_intf_query(SIOCGIFADDR, intf->intf_name, &ifr);
1189 ip = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
1191 (*nids)[i] = LNET_MKNID(nw->nw_id, ip);
1206 * called repeatedly until a match or no more ip range
1208 * ip_range expression
1209 * interface list with all the interface names.
1210 * all the interfaces in the system.
1212 * try to match the ip_range expr to one of the interfaces' IPs in
1213 * the system. If we hit a patch for an interface. Check if that
1214 * interface name is in the list.
1216 * If there are more than one interface in the list, then make sure
1217 * that the IPs for all of these interfaces match the ip ranges
1220 * for each interface in intf_list
1221 * look up the intf name in ifa
1222 * if not there then no match
1223 * check ip obtained from ifa against a match to any of the
1225 * If no match, then fail
1227 * The result is that all the interfaces have to match.
1229 int lustre_lnet_match_ip_to_intf(struct ifaddrs *ifa,
1230 struct list_head *intf_list,
1231 struct list_head *ip_ranges)
1235 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1236 struct ifaddrs *ifaddr = ifa;
1237 struct lustre_lnet_ip_range_descr *ip_range;
1241 * if there are no explicit interfaces, and no ip ranges, then
1242 * configure the first tcp interface we encounter.
1244 if (list_empty(intf_list) && list_empty(ip_ranges)) {
1245 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1246 if (ifaddr->ifa_addr == NULL)
1249 if ((ifaddr->ifa_flags & IFF_UP) == 0)
1252 family = ifaddr->ifa_addr->sa_family;
1253 if (family == AF_INET &&
1254 strcmp(ifaddr->ifa_name, "lo") != 0) {
1255 rc = lustre_lnet_add_intf_descr
1256 (intf_list, ifaddr->ifa_name,
1257 strlen(ifaddr->ifa_name));
1259 if (rc != LUSTRE_CFG_RC_NO_ERR)
1262 return LUSTRE_CFG_RC_MATCH;
1265 return LUSTRE_CFG_RC_NO_MATCH;
1269 * First interface which matches an IP pattern will be used
1271 if (list_empty(intf_list)) {
1273 * no interfaces provided in the rule, but an ip range is
1274 * provided, so try and match an interface to the ip
1277 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1278 if (ifaddr->ifa_addr == NULL)
1281 if ((ifaddr->ifa_flags & IFF_UP) == 0)
1284 family = ifaddr->ifa_addr->sa_family;
1285 if (family == AF_INET) {
1286 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->
1289 list_for_each_entry(ip_range, ip_ranges,
1291 rc = cfs_ip_addr_match(bswap_32(ip),
1292 &ip_range->ipr_expr);
1296 rc = lustre_lnet_add_intf_descr
1297 (intf_list, ifaddr->ifa_name,
1298 strlen(ifaddr->ifa_name));
1300 if (rc != LUSTRE_CFG_RC_NO_ERR)
1306 if (!list_empty(intf_list))
1307 return LUSTRE_CFG_RC_MATCH;
1309 return LUSTRE_CFG_RC_NO_MATCH;
1313 * If an interface is explicitly specified the ip-range might or
1314 * might not be specified. if specified the interface needs to match the
1315 * ip-range. If no ip-range then the interfaces are
1316 * automatically matched if they are all up.
1317 * If > 1 interfaces all the interfaces must match for the NI to
1320 list_for_each_entry_safe(intf_descr, tmp, intf_list, intf_on_network) {
1321 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1322 if (ifaddr->ifa_addr == NULL)
1325 family = ifaddr->ifa_addr->sa_family;
1326 if (family == AF_INET &&
1327 strcmp(intf_descr->intf_name,
1328 ifaddr->ifa_name) == 0)
1332 if (ifaddr == NULL) {
1333 list_del(&intf_descr->intf_on_network);
1334 free_intf_descr(intf_descr);
1338 if ((ifaddr->ifa_flags & IFF_UP) == 0) {
1339 list_del(&intf_descr->intf_on_network);
1340 free_intf_descr(intf_descr);
1344 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr.s_addr;
1347 list_for_each_entry(ip_range, ip_ranges, ipr_entry) {
1348 rc = cfs_ip_addr_match(bswap_32(ip), &ip_range->ipr_expr);
1354 /* no match for this interface */
1355 list_del(&intf_descr->intf_on_network);
1356 free_intf_descr(intf_descr);
1360 return LUSTRE_CFG_RC_MATCH;
1363 int lustre_lnet_resolve_ip2nets_rule(struct lustre_lnet_ip2nets *ip2nets,
1364 lnet_nid_t **nids, __u32 *nnids)
1366 struct ifaddrs *ifa;
1367 int rc = LUSTRE_CFG_RC_NO_ERR;
1369 rc = getifaddrs(&ifa);
1373 rc = lustre_lnet_match_ip_to_intf(ifa,
1374 &ip2nets->ip2nets_net.nw_intflist,
1375 &ip2nets->ip2nets_ip_ranges);
1376 if (rc != LUSTRE_CFG_RC_MATCH) {
1381 rc = lustre_lnet_intf2nids(&ip2nets->ip2nets_net, nids, nnids);
1382 if (rc != LUSTRE_CFG_RC_NO_ERR) {
1393 lustre_lnet_ioctl_config_ni(struct list_head *intf_list,
1394 struct lnet_ioctl_config_lnd_tunables *tunables,
1395 struct cfs_expr_list *global_cpts,
1396 lnet_nid_t *nids, char *err_str)
1399 struct lnet_ioctl_config_ni *conf;
1400 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1401 int rc = LUSTRE_CFG_RC_NO_ERR, i = 0;
1404 struct lnet_dlc_intf_descr *intf_descr;
1406 struct cfs_expr_list *cpt_expr;
1408 list_for_each_entry(intf_descr, intf_list,
1410 if (tunables != NULL)
1411 len = sizeof(struct lnet_ioctl_config_ni) +
1412 sizeof(struct lnet_ioctl_config_lnd_tunables);
1414 len = sizeof(struct lnet_ioctl_config_ni);
1416 data = calloc(1, len);
1418 return LUSTRE_CFG_RC_OUT_OF_MEM;
1419 conf = (struct lnet_ioctl_config_ni*) data;
1420 if (tunables != NULL)
1421 tun = (struct lnet_ioctl_config_lnd_tunables*)
1424 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1425 conf->lic_cfg_hdr.ioc_len = len;
1426 conf->lic_nid = nids[i];
1427 strncpy(conf->lic_ni_intf[0], intf_descr->intf_name,
1430 if (intf_descr->cpt_expr != NULL)
1431 cpt_expr = intf_descr->cpt_expr;
1432 else if (global_cpts != NULL)
1433 cpt_expr = global_cpts;
1437 if (cpt_expr != NULL) {
1438 count = cfs_expr_list_values(cpt_expr,
1439 LNET_MAX_SHOW_NUM_CPT,
1442 memcpy(conf->lic_cpts, cpt_array,
1443 sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1452 conf->lic_ncpts = count;
1454 if (tunables != NULL)
1455 memcpy(tun, tunables, sizeof(*tunables));
1457 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1462 "\"cannot add network: %s\"", strerror(errno));
1470 return LUSTRE_CFG_RC_NO_ERR;
1474 lustre_lnet_config_ip2nets(struct lustre_lnet_ip2nets *ip2nets,
1475 struct lnet_ioctl_config_lnd_tunables *tunables,
1476 struct cfs_expr_list *global_cpts,
1477 int seq_no, struct cYAML **err_rc)
1479 lnet_nid_t *nids = NULL;
1482 char err_str[LNET_MAX_STR_LEN];
1484 snprintf(err_str, sizeof(err_str), "\"success\"");
1489 "\"incomplete ip2nets information\"");
1490 rc = LUSTRE_CFG_RC_BAD_PARAM;
1495 * call below function to resolve the rules into a list of nids.
1496 * The memory is allocated in that function then freed here when
1497 * it's no longer needed.
1499 rc = lustre_lnet_resolve_ip2nets_rule(ip2nets, &nids, &nnids);
1500 if (rc != LUSTRE_CFG_RC_NO_ERR && rc != LUSTRE_CFG_RC_MATCH) {
1503 "\"cannot resolve ip2nets rule\"");
1507 if (list_empty(&ip2nets->ip2nets_net.nw_intflist)) {
1508 snprintf(err_str, sizeof(err_str),
1509 "\"no interfaces match ip2nets rules\"");
1513 rc = lustre_lnet_ioctl_config_ni(&ip2nets->ip2nets_net.nw_intflist,
1514 tunables, global_cpts, nids,
1521 cYAML_build_error(rc, seq_no, ADD_CMD, "ip2nets", err_str, err_rc);
1525 int lustre_lnet_config_ni(struct lnet_dlc_network_descr *nw_descr,
1526 struct cfs_expr_list *global_cpts,
1528 struct lnet_ioctl_config_lnd_tunables *tunables,
1529 int seq_no, struct cYAML **err_rc)
1532 struct lnet_ioctl_config_ni *conf;
1533 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1534 char buf[LNET_MAX_STR_LEN];
1535 int rc = LUSTRE_CFG_RC_NO_ERR;
1536 char err_str[LNET_MAX_STR_LEN];
1537 lnet_nid_t *nids = NULL;
1541 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1544 snprintf(err_str, sizeof(err_str), "\"success\"");
1546 if (ip2net == NULL && nw_descr == NULL) {
1549 "\"mandatory parameters not specified.\"");
1550 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1554 if (ip2net != NULL && strlen(ip2net) >= sizeof(buf)) {
1557 "\"ip2net string too long %d\"",
1558 (int)strlen(ip2net));
1559 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1563 if (ip2net != NULL) {
1564 if (tunables != NULL)
1565 len = sizeof(struct lnet_ioctl_config_ni) +
1566 sizeof(struct lnet_ioctl_config_lnd_tunables);
1568 len = sizeof(struct lnet_ioctl_config_ni);
1569 data = calloc(1, len);
1571 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1574 conf = (struct lnet_ioctl_config_ni*) data;
1575 if (tunables != NULL)
1576 tun = (struct lnet_ioctl_config_lnd_tunables*)
1577 (data + sizeof(*conf));
1579 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1580 conf->lic_cfg_hdr.ioc_len = len;
1581 strncpy(conf->lic_legacy_ip2nets, ip2net,
1584 if (global_cpts != NULL) {
1585 count = cfs_expr_list_values(global_cpts,
1586 LNET_MAX_SHOW_NUM_CPT,
1589 memcpy(conf->lic_cpts, cpt_array,
1590 sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1599 conf->lic_ncpts = count;
1601 if (tunables != NULL)
1602 memcpy(tun, tunables, sizeof(*tunables));
1604 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1609 "\"cannot add network: %s\"", strerror(errno));
1616 if (LNET_NETTYP(nw_descr->nw_id) == LOLND) {
1617 rc = LUSTRE_CFG_RC_NO_ERR;
1621 if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1624 "\"cannot parse net '%s'\"",
1625 libcfs_net2str(nw_descr->nw_id));
1626 rc = LUSTRE_CFG_RC_BAD_PARAM;
1630 if (list_empty(&nw_descr->nw_intflist)) {
1633 "\"no interface name provided\"");
1634 rc = LUSTRE_CFG_RC_BAD_PARAM;
1638 rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids);
1640 snprintf(err_str, sizeof(err_str),
1641 "\"bad parameter\"");
1642 rc = LUSTRE_CFG_RC_BAD_PARAM;
1646 rc = lustre_lnet_ioctl_config_ni(&nw_descr->nw_intflist,
1647 tunables, global_cpts, nids,
1651 if (nw_descr != NULL) {
1652 list_for_each_entry_safe(intf_descr, tmp,
1653 &nw_descr->nw_intflist,
1655 list_del(&intf_descr->intf_on_network);
1656 free_intf_descr(intf_descr);
1660 cYAML_build_error(rc, seq_no, ADD_CMD, "net", err_str, err_rc);
1671 int lustre_lnet_del_ni(struct lnet_dlc_network_descr *nw_descr,
1672 int seq_no, struct cYAML **err_rc)
1674 struct lnet_ioctl_config_ni data;
1675 int rc = LUSTRE_CFG_RC_NO_ERR, i;
1676 char err_str[LNET_MAX_STR_LEN];
1677 lnet_nid_t *nids = NULL;
1679 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1681 snprintf(err_str, sizeof(err_str), "\"success\"");
1683 if (nw_descr == NULL) {
1686 "\"missing mandatory parameter\"");
1687 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1691 if (LNET_NETTYP(nw_descr->nw_id) == LOLND)
1692 return LUSTRE_CFG_RC_NO_ERR;
1694 if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1697 "\"cannot parse net '%s'\"",
1698 libcfs_net2str(nw_descr->nw_id));
1699 rc = LUSTRE_CFG_RC_BAD_PARAM;
1703 rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids);
1705 snprintf(err_str, sizeof(err_str),
1706 "\"bad parameter\"");
1707 rc = LUSTRE_CFG_RC_BAD_PARAM;
1712 * no interfaces just the nw_id is specified
1715 nids = calloc(1, sizeof(*nids));
1717 snprintf(err_str, sizeof(err_str),
1718 "\"out of memory\"");
1719 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1722 nids[0] = LNET_MKNID(nw_descr->nw_id, 0);
1726 for (i = 0; i < nnids; i++) {
1727 LIBCFS_IOC_INIT_V2(data, lic_cfg_hdr);
1728 data.lic_nid = nids[i];
1730 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_LOCAL_NI, &data);
1735 "\"cannot del network: %s\"", strerror(errno));
1739 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
1741 list_del(&intf_descr->intf_on_network);
1742 free_intf_descr(intf_descr);
1746 cYAML_build_error(rc, seq_no, DEL_CMD, "net", err_str, err_rc);
1755 add_msg_stats_to_yaml_blk(struct cYAML *yaml,
1756 struct lnet_ioctl_comm_count *counts)
1758 if (cYAML_create_number(yaml, "put",
1759 counts->ico_put_count)
1762 if (cYAML_create_number(yaml, "get",
1763 counts->ico_get_count)
1766 if (cYAML_create_number(yaml, "reply",
1767 counts->ico_reply_count)
1770 if (cYAML_create_number(yaml, "ack",
1771 counts->ico_ack_count)
1774 if (cYAML_create_number(yaml, "hello",
1775 counts->ico_hello_count)
1782 static struct lnet_ioctl_comm_count *
1783 get_counts(struct lnet_ioctl_element_msg_stats *msg_stats, int idx)
1786 return &msg_stats->im_send_stats;
1788 return &msg_stats->im_recv_stats;
1790 return &msg_stats->im_drop_stats;
1795 int lustre_lnet_show_net(char *nw, int detail, int seq_no,
1796 struct cYAML **show_rc, struct cYAML **err_rc)
1799 struct lnet_ioctl_config_ni *ni_data;
1800 struct lnet_ioctl_config_lnd_tunables *lnd;
1801 struct lnet_ioctl_element_stats *stats;
1802 struct lnet_ioctl_element_msg_stats msg_stats;
1803 __u32 net = LNET_NIDNET(LNET_NID_ANY);
1804 __u32 prev_net = LNET_NIDNET(LNET_NID_ANY);
1805 int rc = LUSTRE_CFG_RC_OUT_OF_MEM, i, j;
1807 struct cYAML *root = NULL, *tunables = NULL,
1808 *net_node = NULL, *interfaces = NULL,
1809 *item = NULL, *first_seq = NULL,
1810 *tmp = NULL, *statistics = NULL;
1811 int str_buf_len = LNET_MAX_SHOW_NUM_CPT * 2;
1812 char str_buf[str_buf_len];
1814 char err_str[LNET_MAX_STR_LEN];
1815 bool exist = false, new_net = true;
1817 size_t buf_size = sizeof(*ni_data) + sizeof(*lnd) + sizeof(*stats);
1819 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1821 buf = calloc(1, buf_size);
1825 ni_data = (struct lnet_ioctl_config_ni *)buf;
1828 net = libcfs_str2net(nw);
1829 if (net == LNET_NIDNET(LNET_NID_ANY)) {
1832 "\"cannot parse net '%s'\"", nw);
1833 rc = LUSTRE_CFG_RC_BAD_PARAM;
1838 root = cYAML_create_object(NULL, NULL);
1842 net_node = cYAML_create_seq(root, "net");
1843 if (net_node == NULL)
1850 memset(buf, 0, buf_size);
1852 LIBCFS_IOC_INIT_V2(*ni_data, lic_cfg_hdr);
1854 * set the ioc_len to the proper value since INIT assumes
1857 ni_data->lic_cfg_hdr.ioc_len = buf_size;
1858 ni_data->lic_idx = i;
1860 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LOCAL_NI, ni_data);
1866 rc_net = LNET_NIDNET(ni_data->lic_nid);
1868 /* filter on provided data */
1869 if (net != LNET_NIDNET(LNET_NID_ANY) &&
1873 /* default rc to -1 in case we hit the goto */
1877 stats = (struct lnet_ioctl_element_stats *)ni_data->lic_bulk;
1878 lnd = (struct lnet_ioctl_config_lnd_tunables *)
1879 (ni_data->lic_bulk + sizeof(*stats));
1881 if (rc_net != prev_net) {
1888 if (!cYAML_create_string(net_node, "net type",
1889 libcfs_net2str(rc_net)))
1892 tmp = cYAML_create_seq(net_node, "local NI(s)");
1898 /* create the tree to be printed. */
1899 item = cYAML_create_seq_item(tmp);
1903 if (first_seq == NULL)
1906 if (cYAML_create_string(item, "nid",
1907 libcfs_nid2str(ni_data->lic_nid)) == NULL)
1910 if (cYAML_create_string(item,
1912 (ni_data->lic_status ==
1913 LNET_NI_STATUS_UP) ?
1914 "up" : "down") == NULL)
1917 /* don't add interfaces unless there is at least one
1919 if (strlen(ni_data->lic_ni_intf[0]) > 0) {
1920 interfaces = cYAML_create_object(item, "interfaces");
1921 if (interfaces == NULL)
1924 for (j = 0; j < LNET_INTERFACES_NUM; j++) {
1925 if (strlen(ni_data->lic_ni_intf[j]) > 0) {
1927 sizeof(str_buf), "%d", j);
1928 if (cYAML_create_string(interfaces,
1930 ni_data->lic_ni_intf[j]) ==
1941 statistics = cYAML_create_object(item, "statistics");
1942 if (statistics == NULL)
1945 if (cYAML_create_number(statistics, "send_count",
1946 stats->iel_send_count)
1950 if (cYAML_create_number(statistics, "recv_count",
1951 stats->iel_recv_count)
1955 if (cYAML_create_number(statistics, "drop_count",
1956 stats->iel_drop_count)
1961 goto continue_without_msg_stats;
1963 LIBCFS_IOC_INIT_V2(msg_stats, im_hdr);
1964 msg_stats.im_hdr.ioc_len = sizeof(msg_stats);
1965 msg_stats.im_idx = i;
1967 rc = l_ioctl(LNET_DEV_ID,
1968 IOC_LIBCFS_GET_LOCAL_NI_MSG_STATS,
1972 goto continue_without_msg_stats;
1975 for (k = 0; k < 3; k++) {
1976 struct lnet_ioctl_comm_count *counts;
1977 struct cYAML *msg_statistics = NULL;
1979 msg_statistics = cYAML_create_object(item,
1980 (char *)gmsg_stat_names[k]);
1981 if (msg_statistics == NULL)
1984 counts = get_counts(&msg_stats, k);
1988 if (!add_msg_stats_to_yaml_blk(msg_statistics,
1993 continue_without_msg_stats:
1994 tunables = cYAML_create_object(item, "tunables");
1998 rc = lustre_net_show_tunables(tunables, &lnd->lt_cmn);
1999 if (rc != LUSTRE_CFG_RC_NO_ERR)
2002 tunables = cYAML_create_object(item, "lnd tunables");
2003 if (tunables == NULL)
2006 rc = lustre_ni_show_tunables(tunables, LNET_NETTYP(rc_net),
2008 if (rc != LUSTRE_CFG_RC_NO_ERR)
2011 if (cYAML_create_number(item, "tcp bonding",
2012 ni_data->lic_tcp_bonding)
2016 if (cYAML_create_number(item, "dev cpt",
2017 ni_data->lic_dev_cpt) == NULL)
2020 /* out put the CPTs in the format: "[x,x,x,...]" */
2021 limit = str_buf + str_buf_len - 3;
2022 pos += snprintf(pos, limit - pos, "\"[");
2023 for (j = 0 ; ni_data->lic_ncpts >= 1 &&
2024 j < ni_data->lic_ncpts &&
2026 pos += snprintf(pos, limit - pos,
2027 "%d", ni_data->lic_cpts[j]);
2028 if ((j + 1) < ni_data->lic_ncpts)
2029 pos += snprintf(pos, limit - pos, ",");
2031 pos += snprintf(pos, 3, "]\"");
2033 if (ni_data->lic_ncpts >= 1 &&
2034 cYAML_create_string(item, "CPT",
2040 /* Print out the net information only if show_rc is not provided */
2041 if (show_rc == NULL)
2042 cYAML_print_tree(root);
2044 if (l_errno != ENOENT) {
2047 "\"cannot get networks: %s\"",
2052 rc = LUSTRE_CFG_RC_NO_ERR;
2054 snprintf(err_str, sizeof(err_str), "\"success\"");
2056 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2057 cYAML_free_tree(root);
2058 } else if (show_rc != NULL && *show_rc != NULL) {
2059 struct cYAML *show_node;
2060 /* find the net node, if one doesn't exist
2061 * then insert one. Otherwise add to the one there
2063 show_node = cYAML_get_object_item(*show_rc, "net");
2064 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2065 cYAML_insert_child(show_node, first_seq);
2068 } else if (show_node == NULL) {
2069 cYAML_insert_sibling((*show_rc)->cy_child,
2073 cYAML_free_tree(root);
2079 cYAML_build_error(rc, seq_no, SHOW_CMD, "net", err_str, err_rc);
2084 int lustre_lnet_enable_routing(int enable, int seq_no, struct cYAML **err_rc)
2086 struct lnet_ioctl_config_data data;
2087 int rc = LUSTRE_CFG_RC_NO_ERR;
2088 char err_str[LNET_MAX_STR_LEN];
2090 snprintf(err_str, sizeof(err_str), "\"success\"");
2092 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2093 data.cfg_config_u.cfg_buffers.buf_enable = (enable) ? 1 : 0;
2095 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIG_RTR, &data);
2100 "\"cannot %s routing %s\"",
2101 (enable) ? "enable" : "disable", strerror(errno));
2106 cYAML_build_error(rc, seq_no,
2107 (enable) ? ADD_CMD : DEL_CMD,
2108 "routing", err_str, err_rc);
2113 int ioctl_set_value(__u32 val, int ioc, char *name,
2114 int seq_no, struct cYAML **err_rc)
2116 struct lnet_ioctl_set_value data;
2117 int rc = LUSTRE_CFG_RC_NO_ERR;
2118 char err_str[LNET_MAX_STR_LEN];
2120 snprintf(err_str, sizeof(err_str), "\"success\"");
2122 LIBCFS_IOC_INIT_V2(data, sv_hdr);
2123 data.sv_value = val;
2125 rc = l_ioctl(LNET_DEV_ID, ioc , &data);
2130 "\"cannot configure %s to %d: %s\"", name,
2131 val, strerror(errno));
2134 cYAML_build_error(rc, seq_no, ADD_CMD, name, err_str, err_rc);
2139 int lustre_lnet_config_max_intf(int max, int seq_no, struct cYAML **err_rc)
2141 int rc = LUSTRE_CFG_RC_NO_ERR;
2142 char err_str[LNET_MAX_STR_LEN];
2143 char val[LNET_MAX_STR_LEN];
2145 snprintf(err_str, sizeof(err_str), "\"success\"");
2147 snprintf(val, sizeof(val), "%d", max);
2149 rc = write_sysfs_file(modparam_path, "lnet_interfaces_max", val,
2150 1, strlen(val) + 1);
2152 snprintf(err_str, sizeof(err_str),
2153 "\"cannot configure max interfaces: %s\"",
2156 cYAML_build_error(rc, seq_no, ADD_CMD, "max_interfaces", err_str, err_rc);
2161 int lustre_lnet_config_discovery(int enable, int seq_no, struct cYAML **err_rc)
2163 int rc = LUSTRE_CFG_RC_NO_ERR;
2164 char err_str[LNET_MAX_STR_LEN];
2165 char val[LNET_MAX_STR_LEN];
2167 snprintf(err_str, sizeof(err_str), "\"success\"");
2169 snprintf(val, sizeof(val), "%u", (enable) ? 0 : 1);
2171 rc = write_sysfs_file(modparam_path, "lnet_peer_discovery_disabled", val,
2172 1, strlen(val) + 1);
2174 snprintf(err_str, sizeof(err_str),
2175 "\"cannot configure discovery: %s\"",
2178 cYAML_build_error(rc, seq_no, ADD_CMD, "discovery", err_str, err_rc);
2184 int lustre_lnet_config_numa_range(int range, int seq_no, struct cYAML **err_rc)
2186 return ioctl_set_value(range, IOC_LIBCFS_SET_NUMA_RANGE,
2187 "numa_range", seq_no, err_rc);
2190 int lustre_lnet_config_buffers(int tiny, int small, int large, int seq_no,
2191 struct cYAML **err_rc)
2193 struct lnet_ioctl_config_data data;
2194 int rc = LUSTRE_CFG_RC_NO_ERR;
2195 char err_str[LNET_MAX_STR_LEN];
2197 snprintf(err_str, sizeof(err_str), "\"success\"");
2199 /* -1 indicates to ignore changes to this field */
2200 if (tiny < -1 || small < -1 || large < -1) {
2203 "\"tiny, small and large must be >= 0\"");
2204 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
2208 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2209 data.cfg_config_u.cfg_buffers.buf_tiny = tiny;
2210 data.cfg_config_u.cfg_buffers.buf_small = small;
2211 data.cfg_config_u.cfg_buffers.buf_large = large;
2213 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_BUF, &data);
2218 "\"cannot configure buffers: %s\"", strerror(errno));
2223 cYAML_build_error(rc, seq_no, ADD_CMD, "buf", err_str, err_rc);
2228 int lustre_lnet_show_routing(int seq_no, struct cYAML **show_rc,
2229 struct cYAML **err_rc)
2231 struct lnet_ioctl_config_data *data;
2232 struct lnet_ioctl_pool_cfg *pool_cfg = NULL;
2233 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2236 char *pools[LNET_NRBPOOLS] = {"tiny", "small", "large"};
2237 int buf_count[LNET_NRBPOOLS] = {0};
2238 struct cYAML *root = NULL, *pools_node = NULL,
2239 *type_node = NULL, *item = NULL, *cpt = NULL,
2240 *first_seq = NULL, *buffers = NULL;
2242 char err_str[LNET_MAX_STR_LEN];
2243 char node_name[LNET_MAX_STR_LEN];
2246 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2248 buf = calloc(1, sizeof(*data) + sizeof(*pool_cfg));
2252 data = (struct lnet_ioctl_config_data *)buf;
2254 root = cYAML_create_object(NULL, NULL);
2258 pools_node = cYAML_create_seq(root, "routing");
2259 if (pools_node == NULL)
2263 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
2264 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
2265 sizeof(struct lnet_ioctl_pool_cfg);
2266 data->cfg_count = i;
2268 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_BUF, data);
2276 pool_cfg = (struct lnet_ioctl_pool_cfg *)data->cfg_bulk;
2278 snprintf(node_name, sizeof(node_name), "cpt[%d]", i);
2279 item = cYAML_create_seq_item(pools_node);
2283 if (first_seq == NULL)
2286 cpt = cYAML_create_object(item, node_name);
2290 /* create the tree and print */
2291 for (j = 0; j < LNET_NRBPOOLS; j++) {
2292 type_node = cYAML_create_object(cpt, pools[j]);
2293 if (type_node == NULL)
2295 if (cYAML_create_number(type_node, "npages",
2296 pool_cfg->pl_pools[j].pl_npages)
2299 if (cYAML_create_number(type_node, "nbuffers",
2300 pool_cfg->pl_pools[j].
2301 pl_nbuffers) == NULL)
2303 if (cYAML_create_number(type_node, "credits",
2304 pool_cfg->pl_pools[j].
2305 pl_credits) == NULL)
2307 if (cYAML_create_number(type_node, "mincredits",
2308 pool_cfg->pl_pools[j].
2309 pl_mincredits) == NULL)
2311 /* keep track of the total count for each of the
2312 * tiny, small and large buffers */
2313 buf_count[j] += pool_cfg->pl_pools[j].pl_nbuffers;
2317 if (pool_cfg != NULL) {
2318 item = cYAML_create_seq_item(pools_node);
2322 if (cYAML_create_number(item, "enable", pool_cfg->pl_routing) ==
2327 /* create a buffers entry in the show. This is necessary so that
2328 * if the YAML output is used to configure a node, the buffer
2329 * configuration takes hold */
2330 buffers = cYAML_create_object(root, "buffers");
2331 if (buffers == NULL)
2334 for (i = 0; i < LNET_NRBPOOLS; i++) {
2335 if (cYAML_create_number(buffers, pools[i], buf_count[i]) == NULL)
2339 if (show_rc == NULL)
2340 cYAML_print_tree(root);
2342 if (l_errno != ENOENT) {
2345 "\"cannot get routing information: %s\"",
2350 rc = LUSTRE_CFG_RC_NO_ERR;
2352 snprintf(err_str, sizeof(err_str), "\"success\"");
2353 rc = LUSTRE_CFG_RC_NO_ERR;
2357 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2358 cYAML_free_tree(root);
2359 } else if (show_rc != NULL && *show_rc != NULL) {
2360 struct cYAML *routing_node;
2361 /* there should exist only one routing block and one
2362 * buffers block. If there already exists a previous one
2363 * then don't add another */
2364 routing_node = cYAML_get_object_item(*show_rc, "routing");
2365 if (routing_node == NULL) {
2366 cYAML_insert_sibling((*show_rc)->cy_child,
2370 cYAML_free_tree(root);
2376 cYAML_build_error(rc, seq_no, SHOW_CMD, "routing", err_str, err_rc);
2381 int lustre_lnet_show_peer(char *knid, int detail, int seq_no,
2382 struct cYAML **show_rc, struct cYAML **err_rc)
2385 * TODO: This function is changing in a future patch to accommodate
2386 * PEER_LIST and proper filtering on any nid of the peer
2388 struct lnet_ioctl_peer_cfg peer_info;
2389 struct lnet_peer_ni_credit_info *lpni_cri;
2390 struct lnet_ioctl_element_stats *lpni_stats;
2391 struct lnet_ioctl_element_msg_stats *msg_stats;
2393 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2398 struct cYAML *root = NULL, *peer = NULL, *peer_ni = NULL,
2399 *first_seq = NULL, *peer_root = NULL, *tmp = NULL,
2400 *msg_statistics = NULL, *statistics = NULL;
2401 char err_str[LNET_MAX_STR_LEN];
2402 lnet_process_id_t *list = NULL;
2406 snprintf(err_str, sizeof(err_str),
2407 "\"out of memory\"");
2409 /* create struct cYAML root object */
2410 root = cYAML_create_object(NULL, NULL);
2414 peer_root = cYAML_create_seq(root, "peer");
2415 if (peer_root == NULL)
2419 size = count * sizeof(lnet_process_id_t);
2420 list = malloc(size);
2426 list[0].nid = libcfs_str2nid(knid);
2430 memset(&peer_info, 0, sizeof(peer_info));
2431 LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2432 peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2433 peer_info.prcfg_size = size;
2434 peer_info.prcfg_bulk = list;
2437 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_LIST,
2439 count = peer_info.prcfg_count;
2443 if (l_errno != E2BIG) {
2446 "\"cannot get peer list: %s\"",
2452 size = peer_info.prcfg_size;
2453 list = malloc(size);
2462 data = malloc(size);
2467 for (i = 0; i < count; i++) {
2469 memset(&peer_info, 0, sizeof(peer_info));
2470 LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2471 peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2472 peer_info.prcfg_prim_nid = list[i].nid;
2473 peer_info.prcfg_size = size;
2474 peer_info.prcfg_bulk = data;
2477 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_NI,
2482 if (l_errno != E2BIG) {
2485 "\"cannot get peer information: %s\"",
2491 size = peer_info.prcfg_size;
2492 data = malloc(size);
2499 peer = cYAML_create_seq_item(peer_root);
2503 if (first_seq == NULL)
2506 lnet_nid_t pnid = peer_info.prcfg_prim_nid;
2507 if (cYAML_create_string(peer, "primary nid",
2508 libcfs_nid2str(pnid))
2511 if (cYAML_create_string(peer, "Multi-Rail",
2512 peer_info.prcfg_mr ? "True" : "False")
2516 * print out the state of the peer only if details are
2520 if (cYAML_create_number(peer, "peer state",
2521 peer_info.prcfg_state)
2526 tmp = cYAML_create_seq(peer, "peer ni");
2531 for (j = 0; j < peer_info.prcfg_count; j++) {
2533 lpni_cri = (void*)nidp + sizeof(nidp);
2534 lpni_stats = (void *)lpni_cri + sizeof(*lpni_cri);
2535 msg_stats = (void *)lpni_stats + sizeof(*lpni_stats);
2536 lpni_data = (void *)msg_stats + sizeof(*msg_stats);
2538 peer_ni = cYAML_create_seq_item(tmp);
2539 if (peer_ni == NULL)
2542 if (cYAML_create_string(peer_ni, "nid",
2543 libcfs_nid2str(*nidp))
2547 if (cYAML_create_string(peer_ni, "state",
2548 lpni_cri->cr_aliveness)
2555 if (cYAML_create_number(peer_ni, "max_ni_tx_credits",
2556 lpni_cri->cr_ni_peer_tx_credits)
2560 if (cYAML_create_number(peer_ni, "available_tx_credits",
2561 lpni_cri->cr_peer_tx_credits)
2565 if (cYAML_create_number(peer_ni, "min_tx_credits",
2566 lpni_cri->cr_peer_min_tx_credits)
2570 if (cYAML_create_number(peer_ni, "tx_q_num_of_buf",
2571 lpni_cri->cr_peer_tx_qnob)
2575 if (cYAML_create_number(peer_ni, "available_rtr_credits",
2576 lpni_cri->cr_peer_rtr_credits)
2580 if (cYAML_create_number(peer_ni, "min_rtr_credits",
2581 lpni_cri->cr_peer_min_rtr_credits)
2585 if (cYAML_create_number(peer_ni, "refcount",
2586 lpni_cri->cr_refcount) == NULL)
2589 statistics = cYAML_create_object(peer_ni, "statistics");
2590 if (statistics == NULL)
2593 if (cYAML_create_number(statistics, "send_count",
2594 lpni_stats->iel_send_count)
2598 if (cYAML_create_number(statistics, "recv_count",
2599 lpni_stats->iel_recv_count)
2603 if (cYAML_create_number(statistics, "drop_count",
2604 lpni_stats->iel_drop_count)
2611 for (k = 0; k < 3; k++) {
2612 struct lnet_ioctl_comm_count *counts;
2614 msg_statistics = cYAML_create_object(peer_ni,
2615 (char *) gmsg_stat_names[k]);
2616 if (msg_statistics == NULL)
2619 counts = get_counts(msg_stats, k);
2623 if (!add_msg_stats_to_yaml_blk(msg_statistics,
2631 /* print output iff show_rc is not provided */
2632 if (show_rc == NULL)
2633 cYAML_print_tree(root);
2635 snprintf(err_str, sizeof(err_str), "\"success\"");
2636 rc = LUSTRE_CFG_RC_NO_ERR;
2641 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2642 cYAML_free_tree(root);
2643 } else if (show_rc != NULL && *show_rc != NULL) {
2644 struct cYAML *show_node;
2645 /* find the peer node, if one doesn't exist then
2646 * insert one. Otherwise add to the one there
2648 show_node = cYAML_get_object_item(*show_rc,
2650 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2651 cYAML_insert_child(show_node, first_seq);
2654 } else if (show_node == NULL) {
2655 cYAML_insert_sibling((*show_rc)->cy_child,
2659 cYAML_free_tree(root);
2665 cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
2671 int lustre_lnet_list_peer(int seq_no,
2672 struct cYAML **show_rc, struct cYAML **err_rc)
2674 struct lnet_ioctl_peer_cfg peer_info;
2675 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2680 struct cYAML *root = NULL, *list_root = NULL, *first_seq = NULL;
2681 char err_str[LNET_MAX_STR_LEN];
2682 lnet_process_id_t *list = NULL;
2684 snprintf(err_str, sizeof(err_str),
2685 "\"out of memory\"");
2687 memset(&peer_info, 0, sizeof(peer_info));
2689 /* create struct cYAML root object */
2690 root = cYAML_create_object(NULL, NULL);
2694 list_root = cYAML_create_seq(root, "peer list");
2695 if (list_root == NULL)
2699 size = count * sizeof(lnet_process_id_t);
2700 list = malloc(size);
2706 LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2707 peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2708 peer_info.prcfg_size = size;
2709 peer_info.prcfg_bulk = list;
2712 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_LIST, &peer_info);
2713 count = peer_info.prcfg_count;
2717 if (l_errno != E2BIG) {
2720 "\"cannot get peer list: %s\"",
2726 size = peer_info.prcfg_size;
2727 list = malloc(size);
2734 /* count is now the actual number of ids in the list. */
2735 for (i = 0; i < count; i++) {
2736 if (cYAML_create_string(list_root, "nid",
2737 libcfs_nid2str(list[i].nid))
2742 /* print output iff show_rc is not provided */
2743 if (show_rc == NULL)
2744 cYAML_print_tree(root);
2746 snprintf(err_str, sizeof(err_str), "\"success\"");
2747 rc = LUSTRE_CFG_RC_NO_ERR;
2752 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2753 cYAML_free_tree(root);
2754 } else if (show_rc != NULL && *show_rc != NULL) {
2755 struct cYAML *show_node;
2756 /* find the peer node, if one doesn't exist then
2757 * insert one. Otherwise add to the one there
2759 show_node = cYAML_get_object_item(*show_rc,
2761 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2762 cYAML_insert_child(show_node, first_seq);
2765 } else if (show_node == NULL) {
2766 cYAML_insert_sibling((*show_rc)->cy_child,
2770 cYAML_free_tree(root);
2776 cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
2782 static void add_to_global(struct cYAML *show_rc, struct cYAML *node,
2785 struct cYAML *show_node;
2787 show_node = cYAML_get_object_item(show_rc, "global");
2788 if (show_node != NULL)
2789 cYAML_insert_sibling(show_node->cy_child,
2792 cYAML_insert_sibling(show_rc->cy_child,
2797 static int build_global_yaml_entry(char *err_str, int err_len, int seq_no,
2798 char *name, __u32 value,
2799 struct cYAML **show_rc,
2800 struct cYAML **err_rc, int err)
2802 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2803 struct cYAML *root = NULL, *global = NULL;
2810 root = cYAML_create_object(NULL, NULL);
2814 global = cYAML_create_object(root, "global");
2818 if (cYAML_create_number(global, name,
2822 if (show_rc == NULL)
2823 cYAML_print_tree(root);
2825 snprintf(err_str, err_len, "\"success\"");
2827 rc = LUSTRE_CFG_RC_NO_ERR;
2830 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2831 cYAML_free_tree(root);
2832 } else if (show_rc != NULL && *show_rc != NULL) {
2833 add_to_global(*show_rc, global, root);
2838 cYAML_build_error(rc, seq_no, SHOW_CMD, "global", err_str, err_rc);
2843 static int ioctl_show_global_values(int ioc, int seq_no, char *name,
2844 struct cYAML **show_rc,
2845 struct cYAML **err_rc)
2847 struct lnet_ioctl_set_value data;
2850 char err_str[LNET_MAX_STR_LEN];
2852 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2854 LIBCFS_IOC_INIT_V2(data, sv_hdr);
2856 rc = l_ioctl(LNET_DEV_ID, ioc, &data);
2861 "\"cannot get %s: %s\"",
2862 name, strerror(l_errno));
2865 return build_global_yaml_entry(err_str, sizeof(err_str), seq_no, name,
2866 data.sv_value, show_rc, err_rc, l_errno);
2869 int lustre_lnet_show_max_intf(int seq_no, struct cYAML **show_rc,
2870 struct cYAML **err_rc)
2872 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2873 char val[LNET_MAX_STR_LEN];
2874 int max_intf = -1, l_errno = 0;
2875 char err_str[LNET_MAX_STR_LEN];
2877 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2879 rc = read_sysfs_file(modparam_path, "lnet_interfaces_max", val,
2883 snprintf(err_str, sizeof(err_str),
2884 "\"cannot get max interfaces: %d\"", rc);
2886 max_intf = atoi(val);
2889 return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
2890 "max_intf", max_intf, show_rc,
2894 int lustre_lnet_show_discovery(int seq_no, struct cYAML **show_rc,
2895 struct cYAML **err_rc)
2897 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2898 char val[LNET_MAX_STR_LEN];
2899 int discovery = -1, l_errno = 0;
2900 char err_str[LNET_MAX_STR_LEN];
2902 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2904 rc = read_sysfs_file(modparam_path, "lnet_peer_discovery_disabled", val,
2908 snprintf(err_str, sizeof(err_str),
2909 "\"cannot get discovery setting: %d\"", rc);
2912 * The kernel stores a discovery disabled value. User space
2913 * shows whether discovery is enabled. So the value must be
2916 discovery = !atoi(val);
2919 return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
2920 "discovery", discovery, show_rc,
2924 int lustre_lnet_show_numa_range(int seq_no, struct cYAML **show_rc,
2925 struct cYAML **err_rc)
2927 return ioctl_show_global_values(IOC_LIBCFS_GET_NUMA_RANGE, seq_no,
2928 "numa_range", show_rc, err_rc);
2931 int lustre_lnet_show_stats(int seq_no, struct cYAML **show_rc,
2932 struct cYAML **err_rc)
2934 struct lnet_ioctl_lnet_stats data;
2937 char err_str[LNET_MAX_STR_LEN];
2938 struct cYAML *root = NULL, *stats = NULL;
2940 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2942 LIBCFS_IOC_INIT_V2(data, st_hdr);
2944 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LNET_STATS, &data);
2949 "\"cannot get lnet statistics: %s\"",
2955 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2957 root = cYAML_create_object(NULL, NULL);
2961 stats = cYAML_create_object(root, "statistics");
2965 if (cYAML_create_number(stats, "msgs_alloc",
2966 data.st_cntrs.msgs_alloc) == NULL)
2969 if (cYAML_create_number(stats, "msgs_max",
2970 data.st_cntrs.msgs_max) == NULL)
2973 if (cYAML_create_number(stats, "errors",
2974 data.st_cntrs.errors) == NULL)
2977 if (cYAML_create_number(stats, "send_count",
2978 data.st_cntrs.send_count) == NULL)
2981 if (cYAML_create_number(stats, "recv_count",
2982 data.st_cntrs.recv_count) == NULL)
2985 if (cYAML_create_number(stats, "route_count",
2986 data.st_cntrs.route_count) == NULL)
2989 if (cYAML_create_number(stats, "drop_count",
2990 data.st_cntrs.drop_count) == NULL)
2993 if (cYAML_create_number(stats, "send_length",
2994 data.st_cntrs.send_length) == NULL)
2997 if (cYAML_create_number(stats, "recv_length",
2998 data.st_cntrs.recv_length) == NULL)
3001 if (cYAML_create_number(stats, "route_length",
3002 data.st_cntrs.route_length) == NULL)
3005 if (cYAML_create_number(stats, "drop_length",
3006 data.st_cntrs.drop_length) == NULL)
3009 if (show_rc == NULL)
3010 cYAML_print_tree(root);
3012 snprintf(err_str, sizeof(err_str), "\"success\"");
3013 rc = LUSTRE_CFG_RC_NO_ERR;
3015 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
3016 cYAML_free_tree(root);
3017 } else if (show_rc != NULL && *show_rc != NULL) {
3018 cYAML_insert_sibling((*show_rc)->cy_child,
3025 cYAML_build_error(rc, seq_no, SHOW_CMD, "statistics", err_str, err_rc);
3030 typedef int (*cmd_handler_t)(struct cYAML *tree,
3031 struct cYAML **show_rc,
3032 struct cYAML **err_rc);
3034 static int handle_yaml_config_route(struct cYAML *tree, struct cYAML **show_rc,
3035 struct cYAML **err_rc)
3037 struct cYAML *net, *gw, *hop, *prio, *seq_no;
3039 net = cYAML_get_object_item(tree, "net");
3040 gw = cYAML_get_object_item(tree, "gateway");
3041 hop = cYAML_get_object_item(tree, "hop");
3042 prio = cYAML_get_object_item(tree, "priority");
3043 seq_no = cYAML_get_object_item(tree, "seq_no");
3045 return lustre_lnet_config_route((net) ? net->cy_valuestring : NULL,
3046 (gw) ? gw->cy_valuestring : NULL,
3047 (hop) ? hop->cy_valueint : -1,
3048 (prio) ? prio->cy_valueint : -1,
3049 (seq_no) ? seq_no->cy_valueint : -1,
3053 static void yaml_free_string_array(char **array, int num)
3056 char **sub_array = array;
3058 for (i = 0; i < num; i++) {
3059 if (*sub_array != NULL)
3069 * 0: <intf_name>['['<expr>']']
3070 * 1: <intf_name>['['<expr>']']
3072 static int yaml_copy_intf_info(struct cYAML *intf_tree,
3073 struct lnet_dlc_network_descr *nw_descr)
3075 struct cYAML *child = NULL;
3076 int intf_num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
3077 struct lnet_dlc_intf_descr *intf_descr, *tmp;
3079 if (intf_tree == NULL || nw_descr == NULL)
3080 return LUSTRE_CFG_RC_BAD_PARAM;
3082 /* now grab all the interfaces and their cpts */
3083 child = intf_tree->cy_child;
3084 while (child != NULL) {
3085 if (child->cy_valuestring == NULL) {
3086 child = child->cy_next;
3090 if (strlen(child->cy_valuestring) >= LNET_MAX_STR_LEN)
3093 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist,
3094 child->cy_valuestring,
3095 strlen(child->cy_valuestring));
3096 if (rc != LUSTRE_CFG_RC_NO_ERR)
3100 child = child->cy_next;
3104 return LUSTRE_CFG_RC_MISSING_PARAM;
3109 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
3111 list_del(&intf_descr->intf_on_network);
3112 free_intf_descr(intf_descr);
3119 yaml_extract_cmn_tunables(struct cYAML *tree,
3120 struct lnet_ioctl_config_lnd_cmn_tunables *tunables,
3121 struct cfs_expr_list **global_cpts)
3123 struct cYAML *tun, *item, *smp;
3126 tun = cYAML_get_object_item(tree, "tunables");
3128 item = cYAML_get_object_item(tun, "peer_timeout");
3130 tunables->lct_peer_timeout = item->cy_valueint;
3131 item = cYAML_get_object_item(tun, "peer_credits");
3133 tunables->lct_peer_tx_credits = item->cy_valueint;
3134 item = cYAML_get_object_item(tun, "peer_buffer_credits");
3136 tunables->lct_peer_rtr_credits = item->cy_valueint;
3137 item = cYAML_get_object_item(tun, "credits");
3139 tunables->lct_max_tx_credits = item->cy_valueint;
3140 smp = cYAML_get_object_item(tun, "CPT");
3142 rc = cfs_expr_list_parse(smp->cy_valuestring,
3143 strlen(smp->cy_valuestring),
3144 0, UINT_MAX, global_cpts);
3146 *global_cpts = NULL;
3156 yaml_extract_tunables(struct cYAML *tree,
3157 struct lnet_ioctl_config_lnd_tunables *tunables,
3158 struct cfs_expr_list **global_cpts,
3163 rc = yaml_extract_cmn_tunables(tree, &tunables->lt_cmn,
3169 lustre_yaml_extract_lnd_tunables(tree, net_type,
3177 * - net type: <net>[<NUM>]
3179 * - nid: <ip>@<net>[<NUM>]
3182 * 0: <intf_name>['['<expr>']']
3183 * 1: <intf_name>['['<expr>']']
3185 * peer_timeout: <NUM>
3186 * peer_credits: <NUM>
3187 * peer_buffer_credits: <NUM>
3190 * peercredits_hiw: <NUM>
3191 * map_on_demand: <NUM>
3192 * concurrent_sends: <NUM>
3193 * fmr_pool_size: <NUM>
3194 * fmr_flush_trigger: <NUM>
3197 * At least one interface is required. If no interfaces are provided the
3198 * network interface can not be configured.
3200 static int handle_yaml_config_ni(struct cYAML *tree, struct cYAML **show_rc,
3201 struct cYAML **err_rc)
3203 struct cYAML *net, *intf, *seq_no, *ip2net = NULL, *local_nis = NULL,
3205 int num_entries = 0, rc;
3206 struct lnet_dlc_network_descr nw_descr;
3207 struct cfs_expr_list *global_cpts = NULL;
3208 struct lnet_ioctl_config_lnd_tunables tunables;
3211 memset(&tunables, 0, sizeof(tunables));
3213 INIT_LIST_HEAD(&nw_descr.network_on_rule);
3214 INIT_LIST_HEAD(&nw_descr.nw_intflist);
3216 ip2net = cYAML_get_object_item(tree, "ip2net");
3217 net = cYAML_get_object_item(tree, "net type");
3219 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
3221 nw_descr.nw_id = LOLND;
3224 * if neither net nor ip2nets are present, then we can not
3225 * configure the network.
3227 if (!net && !ip2net)
3228 return LUSTRE_CFG_RC_MISSING_PARAM;
3230 local_nis = cYAML_get_object_item(tree, "local NI(s)");
3231 if (local_nis == NULL)
3232 return LUSTRE_CFG_RC_MISSING_PARAM;
3234 if (!cYAML_is_sequence(local_nis))
3235 return LUSTRE_CFG_RC_BAD_PARAM;
3237 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
3238 intf = cYAML_get_object_item(item, "interfaces");
3241 num_entries = yaml_copy_intf_info(intf, &nw_descr);
3242 if (num_entries <= 0) {
3243 cYAML_build_error(num_entries, -1, "ni", "add",
3244 "bad interface list",
3246 return LUSTRE_CFG_RC_BAD_PARAM;
3250 found = yaml_extract_tunables(tree, &tunables, &global_cpts,
3251 LNET_NETTYP(nw_descr.nw_id));
3252 seq_no = cYAML_get_object_item(tree, "seq_no");
3254 rc = lustre_lnet_config_ni(&nw_descr,
3256 (ip2net) ? ip2net->cy_valuestring : NULL,
3257 (found) ? &tunables: NULL,
3258 (seq_no) ? seq_no->cy_valueint : -1,
3261 if (global_cpts != NULL)
3262 cfs_expr_list_free(global_cpts);
3269 * - net-spec: <tcp|o2ib|gni>[NUM]
3271 * 0: <intf name>['['<expr>']']
3272 * 1: <intf name>['['<expr>']']
3274 * 0: <expr.expr.expr.expr>
3275 * 1: <expr.expr.expr.expr>
3277 static int handle_yaml_config_ip2nets(struct cYAML *tree,
3278 struct cYAML **show_rc,
3279 struct cYAML **err_rc)
3281 struct cYAML *net, *ip_range, *item = NULL, *intf = NULL,
3283 struct lustre_lnet_ip2nets ip2nets;
3284 struct lustre_lnet_ip_range_descr *ip_range_descr = NULL,
3286 int rc = LUSTRE_CFG_RC_NO_ERR;
3287 struct cfs_expr_list *global_cpts = NULL;
3288 struct cfs_expr_list *el, *el_tmp;
3289 struct lnet_ioctl_config_lnd_tunables tunables;
3290 struct lnet_dlc_intf_descr *intf_descr, *intf_tmp;
3293 memset(&tunables, 0, sizeof(tunables));
3295 /* initialize all lists */
3296 INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
3297 INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
3298 INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
3300 net = cYAML_get_object_item(tree, "net-spec");
3302 return LUSTRE_CFG_RC_BAD_PARAM;
3304 if (net != NULL && net->cy_valuestring == NULL)
3305 return LUSTRE_CFG_RC_BAD_PARAM;
3307 /* assign the network id */
3308 ip2nets.ip2nets_net.nw_id = libcfs_str2net(net->cy_valuestring);
3309 if (ip2nets.ip2nets_net.nw_id == LNET_NID_ANY)
3310 return LUSTRE_CFG_RC_BAD_PARAM;
3312 seq_no = cYAML_get_object_item(tree, "seq_no");
3314 intf = cYAML_get_object_item(tree, "interfaces");
3316 rc = yaml_copy_intf_info(intf, &ip2nets.ip2nets_net);
3318 return LUSTRE_CFG_RC_BAD_PARAM;
3321 ip_range = cYAML_get_object_item(tree, "ip-range");
3322 if (ip_range != NULL) {
3323 item = ip_range->cy_child;
3324 while (item != NULL) {
3325 if (item->cy_valuestring == NULL) {
3326 item = item->cy_next;
3330 rc = lustre_lnet_add_ip_range(&ip2nets.ip2nets_ip_ranges,
3331 item->cy_valuestring);
3333 if (rc != LUSTRE_CFG_RC_NO_ERR)
3336 item = item->cy_next;
3340 found = yaml_extract_tunables(tree, &tunables, &global_cpts,
3341 LNET_NETTYP(ip2nets.ip2nets_net.nw_id));
3343 rc = lustre_lnet_config_ip2nets(&ip2nets,
3344 (found) ? &tunables : NULL,
3346 (seq_no) ? seq_no->cy_valueint : -1,
3350 * don't stop because there was no match. Continue processing the
3351 * rest of the rules. If non-match then nothing is configured
3353 if (rc == LUSTRE_CFG_RC_NO_MATCH)
3354 rc = LUSTRE_CFG_RC_NO_ERR;
3356 list_for_each_entry_safe(intf_descr, intf_tmp,
3357 &ip2nets.ip2nets_net.nw_intflist,
3359 list_del(&intf_descr->intf_on_network);
3360 free_intf_descr(intf_descr);
3363 list_for_each_entry_safe(ip_range_descr, tmp,
3364 &ip2nets.ip2nets_ip_ranges,
3366 list_del(&ip_range_descr->ipr_entry);
3367 list_for_each_entry_safe(el, el_tmp, &ip_range_descr->ipr_expr,
3369 list_del(&el->el_link);
3370 cfs_expr_list_free(el);
3372 free(ip_range_descr);
3378 static int handle_yaml_del_ni(struct cYAML *tree, struct cYAML **show_rc,
3379 struct cYAML **err_rc)
3381 struct cYAML *net = NULL, *intf = NULL, *seq_no = NULL, *item = NULL,
3383 int num_entries, rc;
3384 struct lnet_dlc_network_descr nw_descr;
3386 INIT_LIST_HEAD(&nw_descr.network_on_rule);
3387 INIT_LIST_HEAD(&nw_descr.nw_intflist);
3389 net = cYAML_get_object_item(tree, "net type");
3391 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
3393 local_nis = cYAML_get_object_item(tree, "local NI(s)");
3394 if (local_nis == NULL)
3395 return LUSTRE_CFG_RC_MISSING_PARAM;
3397 if (!cYAML_is_sequence(local_nis))
3398 return LUSTRE_CFG_RC_BAD_PARAM;
3400 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
3401 intf = cYAML_get_object_item(item, "interfaces");
3404 num_entries = yaml_copy_intf_info(intf, &nw_descr);
3405 if (num_entries <= 0) {
3406 cYAML_build_error(num_entries, -1, "ni", "add",
3407 "bad interface list",
3409 return LUSTRE_CFG_RC_BAD_PARAM;
3413 seq_no = cYAML_get_object_item(tree, "seq_no");
3415 rc = lustre_lnet_del_ni((net) ? &nw_descr : NULL,
3416 (seq_no) ? seq_no->cy_valueint : -1,
3422 static int yaml_copy_peer_nids(struct cYAML *tree, char ***nidsppp, bool del)
3424 struct cYAML *nids_entry = NULL, *child = NULL, *entry = NULL,
3427 int num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
3429 prim_nid = cYAML_get_object_item(tree, "primary nid");
3430 if (!prim_nid || !prim_nid->cy_valuestring)
3431 return LUSTRE_CFG_RC_MISSING_PARAM;
3433 nids_entry = cYAML_get_object_item(tree, "peer ni");
3434 if (cYAML_is_sequence(nids_entry)) {
3435 while (cYAML_get_next_seq_item(nids_entry, &child)) {
3436 entry = cYAML_get_object_item(child, "nid");
3437 /* don't count an empty entry */
3438 if (!entry || !entry->cy_valuestring)
3441 if ((strcmp(entry->cy_valuestring, prim_nid->cy_valuestring)
3444 * primary nid is present in the list of
3445 * nids so that means we want to delete
3446 * the entire peer, so no need to go
3447 * further. Just delete the entire peer.
3457 return LUSTRE_CFG_RC_MISSING_PARAM;
3459 nids = calloc(sizeof(*nids) * num, 1);
3461 return LUSTRE_CFG_RC_OUT_OF_MEM;
3463 /* now grab all the nids */
3466 while (cYAML_get_next_seq_item(nids_entry, &child)) {
3467 entry = cYAML_get_object_item(child, "nid");
3468 if (!entry || !entry->cy_valuestring)
3471 nids[num] = calloc(strlen(entry->cy_valuestring) + 1, 1);
3473 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3476 strncpy(nids[num], entry->cy_valuestring,
3477 strlen(entry->cy_valuestring));
3487 yaml_free_string_array(nids, num);
3492 static int handle_yaml_config_peer(struct cYAML *tree, struct cYAML **show_rc,
3493 struct cYAML **err_rc)
3497 struct cYAML *seq_no, *prim_nid, *non_mr;
3499 num = yaml_copy_peer_nids(tree, &nids, false);
3503 seq_no = cYAML_get_object_item(tree, "seq_no");
3504 prim_nid = cYAML_get_object_item(tree, "primary nid");
3505 non_mr = cYAML_get_object_item(tree, "non_mr");
3507 rc = lustre_lnet_config_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
3509 (non_mr) ? false : true,
3510 (seq_no) ? seq_no->cy_valueint : -1,
3513 yaml_free_string_array(nids, num);
3517 static int handle_yaml_del_peer(struct cYAML *tree, struct cYAML **show_rc,
3518 struct cYAML **err_rc)
3522 struct cYAML *seq_no, *prim_nid;
3524 num = yaml_copy_peer_nids(tree, &nids, true);
3528 seq_no = cYAML_get_object_item(tree, "seq_no");
3529 prim_nid = cYAML_get_object_item(tree, "primary nid");
3531 rc = lustre_lnet_del_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
3533 (seq_no) ? seq_no->cy_valueint : -1,
3536 yaml_free_string_array(nids, num);
3540 static int handle_yaml_config_buffers(struct cYAML *tree,
3541 struct cYAML **show_rc,
3542 struct cYAML **err_rc)
3545 struct cYAML *tiny, *small, *large, *seq_no;
3547 tiny = cYAML_get_object_item(tree, "tiny");
3548 small = cYAML_get_object_item(tree, "small");
3549 large = cYAML_get_object_item(tree, "large");
3550 seq_no = cYAML_get_object_item(tree, "seq_no");
3552 rc = lustre_lnet_config_buffers((tiny) ? tiny->cy_valueint : -1,
3553 (small) ? small->cy_valueint : -1,
3554 (large) ? large->cy_valueint : -1,
3555 (seq_no) ? seq_no->cy_valueint : -1,
3561 static int handle_yaml_config_routing(struct cYAML *tree,
3562 struct cYAML **show_rc,
3563 struct cYAML **err_rc)
3565 int rc = LUSTRE_CFG_RC_NO_ERR;
3566 struct cYAML *seq_no, *enable;
3568 seq_no = cYAML_get_object_item(tree, "seq_no");
3569 enable = cYAML_get_object_item(tree, "enable");
3572 rc = lustre_lnet_enable_routing(enable->cy_valueint,
3574 seq_no->cy_valueint : -1,
3581 static int handle_yaml_del_route(struct cYAML *tree, struct cYAML **show_rc,
3582 struct cYAML **err_rc)
3586 struct cYAML *seq_no;
3588 net = cYAML_get_object_item(tree, "net");
3589 gw = cYAML_get_object_item(tree, "gateway");
3590 seq_no = cYAML_get_object_item(tree, "seq_no");
3592 return lustre_lnet_del_route((net) ? net->cy_valuestring : NULL,
3593 (gw) ? gw->cy_valuestring : NULL,
3594 (seq_no) ? seq_no->cy_valueint : -1,
3598 static int handle_yaml_del_routing(struct cYAML *tree, struct cYAML **show_rc,
3599 struct cYAML **err_rc)
3601 struct cYAML *seq_no;
3603 seq_no = cYAML_get_object_item(tree, "seq_no");
3605 return lustre_lnet_enable_routing(0, (seq_no) ?
3606 seq_no->cy_valueint : -1,
3610 static int handle_yaml_show_route(struct cYAML *tree, struct cYAML **show_rc,
3611 struct cYAML **err_rc)
3617 struct cYAML *detail;
3618 struct cYAML *seq_no;
3620 net = cYAML_get_object_item(tree, "net");
3621 gw = cYAML_get_object_item(tree, "gateway");
3622 hop = cYAML_get_object_item(tree, "hop");
3623 prio = cYAML_get_object_item(tree, "priority");
3624 detail = cYAML_get_object_item(tree, "detail");
3625 seq_no = cYAML_get_object_item(tree, "seq_no");
3627 return lustre_lnet_show_route((net) ? net->cy_valuestring : NULL,
3628 (gw) ? gw->cy_valuestring : NULL,
3629 (hop) ? hop->cy_valueint : -1,
3630 (prio) ? prio->cy_valueint : -1,
3631 (detail) ? detail->cy_valueint : 0,
3632 (seq_no) ? seq_no->cy_valueint : -1,
3637 static int handle_yaml_show_net(struct cYAML *tree, struct cYAML **show_rc,
3638 struct cYAML **err_rc)
3640 struct cYAML *net, *detail, *seq_no;
3642 net = cYAML_get_object_item(tree, "net");
3643 detail = cYAML_get_object_item(tree, "detail");
3644 seq_no = cYAML_get_object_item(tree, "seq_no");
3646 return lustre_lnet_show_net((net) ? net->cy_valuestring : NULL,
3647 (detail) ? detail->cy_valueint : 0,
3648 (seq_no) ? seq_no->cy_valueint : -1,
3653 static int handle_yaml_show_routing(struct cYAML *tree, struct cYAML **show_rc,
3654 struct cYAML **err_rc)
3656 struct cYAML *seq_no;
3658 seq_no = cYAML_get_object_item(tree, "seq_no");
3660 return lustre_lnet_show_routing((seq_no) ? seq_no->cy_valueint : -1,
3664 static int handle_yaml_show_peers(struct cYAML *tree, struct cYAML **show_rc,
3665 struct cYAML **err_rc)
3667 struct cYAML *seq_no, *nid, *detail;
3669 seq_no = cYAML_get_object_item(tree, "seq_no");
3670 detail = cYAML_get_object_item(tree, "detail");
3671 nid = cYAML_get_object_item(tree, "nid");
3673 return lustre_lnet_show_peer((nid) ? nid->cy_valuestring : NULL,
3674 (detail) ? detail->cy_valueint : 0,
3675 (seq_no) ? seq_no->cy_valueint : -1,
3679 static int handle_yaml_show_stats(struct cYAML *tree, struct cYAML **show_rc,
3680 struct cYAML **err_rc)
3682 struct cYAML *seq_no;
3684 seq_no = cYAML_get_object_item(tree, "seq_no");
3686 return lustre_lnet_show_stats((seq_no) ? seq_no->cy_valueint : -1,
3690 static int handle_yaml_config_global_settings(struct cYAML *tree,
3691 struct cYAML **show_rc,
3692 struct cYAML **err_rc)
3694 struct cYAML *max_intf, *numa, *discovery, *seq_no;
3697 seq_no = cYAML_get_object_item(tree, "seq_no");
3698 max_intf = cYAML_get_object_item(tree, "max_intf");
3700 rc = lustre_lnet_config_max_intf(max_intf->cy_valueint,
3701 seq_no ? seq_no->cy_valueint
3705 numa = cYAML_get_object_item(tree, "numa_range");
3707 rc = lustre_lnet_config_numa_range(numa->cy_valueint,
3708 seq_no ? seq_no->cy_valueint
3712 discovery = cYAML_get_object_item(tree, "discovery");
3714 rc = lustre_lnet_config_discovery(discovery->cy_valueint,
3715 seq_no ? seq_no->cy_valueint
3722 static int handle_yaml_del_global_settings(struct cYAML *tree,
3723 struct cYAML **show_rc,
3724 struct cYAML **err_rc)
3726 struct cYAML *max_intf, *numa, *discovery, *seq_no;
3729 seq_no = cYAML_get_object_item(tree, "seq_no");
3730 max_intf = cYAML_get_object_item(tree, "max_intf");
3732 rc = lustre_lnet_config_max_intf(LNET_INTERFACES_MAX_DEFAULT,
3733 seq_no ? seq_no->cy_valueint
3737 numa = cYAML_get_object_item(tree, "numa_range");
3739 rc = lustre_lnet_config_numa_range(0,
3740 seq_no ? seq_no->cy_valueint
3744 /* peer discovery is enabled by default */
3745 discovery = cYAML_get_object_item(tree, "discovery");
3747 rc = lustre_lnet_config_discovery(1,
3748 seq_no ? seq_no->cy_valueint
3755 static int handle_yaml_show_global_settings(struct cYAML *tree,
3756 struct cYAML **show_rc,
3757 struct cYAML **err_rc)
3759 struct cYAML *max_intf, *numa, *discovery, *seq_no;
3762 seq_no = cYAML_get_object_item(tree, "seq_no");
3763 max_intf = cYAML_get_object_item(tree, "max_intf");
3765 rc = lustre_lnet_show_max_intf(seq_no ? seq_no->cy_valueint
3769 numa = cYAML_get_object_item(tree, "numa_range");
3771 rc = lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint
3775 discovery = cYAML_get_object_item(tree, "discovery");
3777 rc = lustre_lnet_show_discovery(seq_no ? seq_no->cy_valueint
3784 static int handle_yaml_ping(struct cYAML *tree, struct cYAML **show_rc,
3785 struct cYAML **err_rc)
3787 struct cYAML *seq_no, *nid, *timeout;
3789 seq_no = cYAML_get_object_item(tree, "seq_no");
3790 nid = cYAML_get_object_item(tree, "primary nid");
3791 timeout = cYAML_get_object_item(tree, "timeout");
3793 return lustre_lnet_ping_nid((nid) ? nid->cy_valuestring : NULL,
3794 (timeout) ? timeout->cy_valueint : 1000,
3795 (seq_no) ? seq_no->cy_valueint : -1,
3799 static int handle_yaml_discover(struct cYAML *tree, struct cYAML **show_rc,
3800 struct cYAML **err_rc)
3802 struct cYAML *seq_no, *nid, *force;
3804 seq_no = cYAML_get_object_item(tree, "seq_no");
3805 nid = cYAML_get_object_item(tree, "primary nid");
3806 force = cYAML_get_object_item(tree, "force");
3808 return lustre_lnet_discover_nid((nid) ? nid->cy_valuestring : NULL,
3809 (force) ? force->cy_valueint : 0,
3810 (seq_no) ? seq_no->cy_valueint : -1,
3814 static int handle_yaml_no_op()
3816 return LUSTRE_CFG_RC_NO_ERR;
3819 struct lookup_cmd_hdlr_tbl {
3824 static struct lookup_cmd_hdlr_tbl lookup_config_tbl[] = {
3825 { .name = "route", .cb = handle_yaml_config_route },
3826 { .name = "net", .cb = handle_yaml_config_ni },
3827 { .name = "ip2nets", .cb = handle_yaml_config_ip2nets },
3828 { .name = "peer", .cb = handle_yaml_config_peer },
3829 { .name = "routing", .cb = handle_yaml_config_routing },
3830 { .name = "buffers", .cb = handle_yaml_config_buffers },
3831 { .name = "statistics", .cb = handle_yaml_no_op },
3832 { .name = "global", .cb = handle_yaml_config_global_settings},
3833 { .name = "ping", .cb = handle_yaml_no_op },
3834 { .name = "discover", .cb = handle_yaml_no_op },
3837 static struct lookup_cmd_hdlr_tbl lookup_del_tbl[] = {
3838 { .name = "route", .cb = handle_yaml_del_route },
3839 { .name = "net", .cb = handle_yaml_del_ni },
3840 { .name = "ip2nets", .cb = handle_yaml_no_op },
3841 { .name = "peer", .cb = handle_yaml_del_peer },
3842 { .name = "routing", .cb = handle_yaml_del_routing },
3843 { .name = "buffers", .cb = handle_yaml_no_op },
3844 { .name = "statistics", .cb = handle_yaml_no_op },
3845 { .name = "global", .cb = handle_yaml_del_global_settings},
3846 { .name = "ping", .cb = handle_yaml_no_op },
3847 { .name = "discover", .cb = handle_yaml_no_op },
3850 static struct lookup_cmd_hdlr_tbl lookup_show_tbl[] = {
3851 { .name = "route", .cb = handle_yaml_show_route },
3852 { .name = "net", .cb = handle_yaml_show_net },
3853 { .name = "peer", .cb = handle_yaml_show_peers },
3854 { .name = "ip2nets", .cb = handle_yaml_no_op },
3855 { .name = "routing", .cb = handle_yaml_show_routing },
3856 { .name = "buffers", .cb = handle_yaml_show_routing },
3857 { .name = "statistics", .cb = handle_yaml_show_stats },
3858 { .name = "global", .cb = handle_yaml_show_global_settings},
3859 { .name = "ping", .cb = handle_yaml_no_op },
3860 { .name = "discover", .cb = handle_yaml_no_op },
3863 static struct lookup_cmd_hdlr_tbl lookup_exec_tbl[] = {
3864 { .name = "route", .cb = handle_yaml_no_op },
3865 { .name = "net", .cb = handle_yaml_no_op },
3866 { .name = "peer", .cb = handle_yaml_no_op },
3867 { .name = "ip2nets", .cb = handle_yaml_no_op },
3868 { .name = "routing", .cb = handle_yaml_no_op },
3869 { .name = "buffers", .cb = handle_yaml_no_op },
3870 { .name = "statistics", .cb = handle_yaml_no_op },
3871 { .name = "global", .cb = handle_yaml_no_op },
3872 { .name = "ping", .cb = handle_yaml_ping },
3873 { .name = "discover", .cb = handle_yaml_discover },
3876 static cmd_handler_t lookup_fn(char *key,
3877 struct lookup_cmd_hdlr_tbl *tbl)
3883 for (i = 0; tbl[i].name != NULL; i++) {
3884 if (strncmp(key, tbl[i].name, strlen(tbl[i].name)) == 0)
3891 static int lustre_yaml_cb_helper(char *f, struct lookup_cmd_hdlr_tbl *table,
3892 struct cYAML **show_rc, struct cYAML **err_rc)
3894 struct cYAML *tree, *item = NULL, *head, *child;
3896 char err_str[LNET_MAX_STR_LEN];
3897 int rc = LUSTRE_CFG_RC_NO_ERR, return_rc = LUSTRE_CFG_RC_NO_ERR;
3899 tree = cYAML_build_tree(f, NULL, 0, err_rc, false);
3901 return LUSTRE_CFG_RC_BAD_PARAM;
3903 child = tree->cy_child;
3904 while (child != NULL) {
3905 cb = lookup_fn(child->cy_string, table);
3907 snprintf(err_str, sizeof(err_str),
3908 "\"call back for '%s' not found\"",
3910 cYAML_build_error(LUSTRE_CFG_RC_BAD_PARAM, -1,
3911 "yaml", "helper", err_str, err_rc);
3915 if (cYAML_is_sequence(child)) {
3916 while ((head = cYAML_get_next_seq_item(child, &item))
3918 rc = cb(head, show_rc, err_rc);
3919 if (rc != LUSTRE_CFG_RC_NO_ERR)
3923 rc = cb(child, show_rc, err_rc);
3924 if (rc != LUSTRE_CFG_RC_NO_ERR)
3928 child = child->cy_next;
3932 cYAML_free_tree(tree);
3937 int lustre_yaml_config(char *f, struct cYAML **err_rc)
3939 return lustre_yaml_cb_helper(f, lookup_config_tbl,
3943 int lustre_yaml_del(char *f, struct cYAML **err_rc)
3945 return lustre_yaml_cb_helper(f, lookup_del_tbl,
3949 int lustre_yaml_show(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
3951 return lustre_yaml_cb_helper(f, lookup_show_tbl,
3955 int lustre_yaml_exec(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
3957 return lustre_yaml_cb_helper(f, lookup_exec_tbl,