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/"
65 const char *gmsg_stat_names[] = {"sent_stats", "received_stats",
69 * lustre_lnet_ip_range_descr
70 * Describes an IP range.
71 * Each octect is an expression
73 struct lustre_lnet_ip_range_descr {
74 struct list_head ipr_entry;
75 struct list_head ipr_expr;
80 * Describes an ip2nets rule. This can be on a list of rules.
82 struct lustre_lnet_ip2nets {
83 struct lnet_dlc_network_descr ip2nets_net;
84 struct list_head ip2nets_ip_ranges;
87 int open_sysfs_file(const char *path, const char *attr, const int mode)
90 char filename[LNET_MAX_STR_LEN];
92 if (strlen(path) + strlen(attr) >= LNET_MAX_STR_LEN)
95 snprintf(filename, sizeof(filename), "%s%s",
98 fd = open(filename, mode);
103 static int read_sysfs_file(const char *path, const char *attr,
104 void *val, const size_t size, const int nelem)
107 int rc = LUSTRE_CFG_RC_GENERIC_ERR;
109 fd = open_sysfs_file(path, attr, O_RDONLY);
111 return LUSTRE_CFG_RC_NO_MATCH;
113 if (read(fd, val, size * nelem) == -1)
116 rc = LUSTRE_CFG_RC_NO_ERR;
123 static int write_sysfs_file(const char *path, const char *attr,
124 void *val, const size_t size, const int nelem)
127 int rc = LUSTRE_CFG_RC_GENERIC_ERR;
129 fd = open_sysfs_file(path, attr, O_WRONLY | O_TRUNC);
131 return LUSTRE_CFG_RC_NO_MATCH;
133 if (write(fd, val, size * nelem) == -1)
136 rc = LUSTRE_CFG_RC_NO_ERR;
145 * frees the memory allocated for an intf descriptor.
147 void free_intf_descr(struct lnet_dlc_intf_descr *intf_descr)
152 if (intf_descr->cpt_expr != NULL)
153 cfs_expr_list_free(intf_descr->cpt_expr);
158 * lustre_lnet_add_ip_range
160 * given a string of the format:
161 * <expr.expr.expr.expr> parse each expr into
162 * a lustre_lnet_ip_range_descr structure and insert on the list.
164 * This function is called from
165 * YAML on each ip-range.
166 * As a result of lnetctl command
167 * When building a NID or P2P selection rules
169 int lustre_lnet_add_ip_range(struct list_head *list, char *str_ip_range)
171 struct lustre_lnet_ip_range_descr *ip_range;
174 ip_range = calloc(1, sizeof(*ip_range));
175 if (ip_range == NULL)
176 return LUSTRE_CFG_RC_OUT_OF_MEM;
178 INIT_LIST_HEAD(&ip_range->ipr_entry);
179 INIT_LIST_HEAD(&ip_range->ipr_expr);
181 rc = cfs_ip_addr_parse(str_ip_range, strlen(str_ip_range),
182 &ip_range->ipr_expr);
184 return LUSTRE_CFG_RC_BAD_PARAM;
186 list_add_tail(&ip_range->ipr_entry, list);
188 return LUSTRE_CFG_RC_NO_ERR;
191 int lustre_lnet_add_intf_descr(struct list_head *list, char *intf, int len)
193 char *open_sq_bracket = NULL, *close_sq_bracket = NULL,
195 struct lnet_dlc_intf_descr *intf_descr = NULL;
197 char intf_string[LNET_MAX_STR_LEN];
199 if (len >= LNET_MAX_STR_LEN)
200 return LUSTRE_CFG_RC_BAD_PARAM;
202 strncpy(intf_string, intf, len);
203 intf_string[len] = '\0';
205 intf_descr = calloc(1, sizeof(*intf_descr));
206 if (intf_descr == NULL)
207 return LUSTRE_CFG_RC_OUT_OF_MEM;
209 INIT_LIST_HEAD(&intf_descr->intf_on_network);
211 intf_name = intf_string;
212 open_sq_bracket = strchr(intf_string, '[');
213 if (open_sq_bracket != NULL) {
214 close_sq_bracket = strchr(intf_string, ']');
215 if (close_sq_bracket == NULL) {
217 return LUSTRE_CFG_RC_BAD_PARAM;
219 rc = cfs_expr_list_parse(open_sq_bracket,
220 strlen(open_sq_bracket), 0, UINT_MAX,
221 &intf_descr->cpt_expr);
224 return LUSTRE_CFG_RC_BAD_PARAM;
226 strncpy(intf_descr->intf_name, intf_name,
227 open_sq_bracket - intf_name);
228 intf_descr->intf_name[open_sq_bracket - intf_name] = '\0';
230 strcpy(intf_descr->intf_name, intf_name);
231 intf_descr->cpt_expr = NULL;
234 list_add_tail(&intf_descr->intf_on_network, list);
236 return LUSTRE_CFG_RC_NO_ERR;
239 void lustre_lnet_init_nw_descr(struct lnet_dlc_network_descr *nw_descr)
241 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);
402 void lustre_lnet_config_lib_uninit(void)
404 unregister_ioc_dev(LNET_DEV_ID);
407 int lustre_lnet_config_ni_system(bool up, bool load_ni_from_mod,
408 int seq_no, struct cYAML **err_rc)
410 struct libcfs_ioctl_data data;
413 char err_str[LNET_MAX_STR_LEN];
415 snprintf(err_str, sizeof(err_str), "\"Success\"");
417 LIBCFS_IOC_INIT(data);
419 /* Reverse logic is used here in order not to change
420 * the lctl utility */
421 data.ioc_flags = load_ni_from_mod ? 0 : 1;
423 opc = up ? IOC_LIBCFS_CONFIGURE : IOC_LIBCFS_UNCONFIGURE;
425 rc = l_ioctl(LNET_DEV_ID, opc, &data);
429 "\"LNet %s error: %s\"", (up) ? "configure" :
430 "unconfigure", strerror(errno));
434 cYAML_build_error(rc, seq_no, (up) ? CONFIG_CMD : UNCONFIG_CMD,
435 "lnet", err_str, err_rc);
440 static lnet_nid_t *allocate_create_nid_array(char **nids, __u32 num_nids,
443 lnet_nid_t *array = NULL;
446 if (!nids || num_nids == 0) {
447 snprintf(err_str, LNET_MAX_STR_LEN, "no NIDs to add");
451 array = calloc(sizeof(*array) * num_nids, 1);
453 snprintf(err_str, LNET_MAX_STR_LEN, "out of memory");
457 for (i = 0; i < num_nids; i++) {
458 array[i] = libcfs_str2nid(nids[i]);
459 if (array[i] == LNET_NID_ANY) {
461 snprintf(err_str, LNET_MAX_STR_LEN,
471 static int dispatch_peer_ni_cmd(lnet_nid_t pnid, lnet_nid_t nid, __u32 cmd,
472 struct lnet_ioctl_peer_cfg *data,
473 char *err_str, char *cmd_str)
477 data->prcfg_prim_nid = pnid;
478 data->prcfg_cfg_nid = nid;
480 rc = l_ioctl(LNET_DEV_ID, cmd, data);
485 "\"cannot %s peer ni: %s\"",
486 (cmd_str) ? cmd_str : "add", strerror(errno));
492 static int infra_ping_nid(char *ping_nids, char *oper, int param, int ioc_call,
493 int seq_no, struct cYAML **show_rc,
494 struct cYAML **err_rc)
497 struct lnet_ioctl_ping_data ping;
498 struct cYAML *root = NULL, *ping_node = NULL, *item = NULL,
499 *first_seq = NULL, *tmp = NULL, *peer_ni = NULL;
500 struct lnet_process_id id;
501 char err_str[LNET_MAX_STR_LEN] = {0};
502 char *sep, *token, *end;
505 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
509 len = (sizeof(struct lnet_process_id) * LNET_INTERFACES_MAX_DEFAULT);
511 data = calloc(1, len);
515 /* create struct cYAML root object */
516 root = cYAML_create_object(NULL, NULL);
520 ping_node = cYAML_create_seq(root, oper);
521 if (ping_node == NULL)
524 /* tokenise each nid in string ping_nids */
525 token = strtok(ping_nids, ",");
528 item = cYAML_create_seq_item(ping_node);
532 if (first_seq == NULL)
535 /* check if '-' is a part of NID, token */
536 sep = strchr(token, '-');
538 id.pid = LNET_PID_ANY;
539 /* if no net is specified, libcfs_str2nid() will assume tcp */
540 id.nid = libcfs_str2nid(token);
541 if (id.nid == LNET_NID_ANY) {
542 snprintf(err_str, sizeof(err_str),
543 "\"cannot parse NID '%s'\"",
545 rc = LUSTRE_CFG_RC_BAD_PARAM;
546 cYAML_build_error(rc, seq_no, MANAGE_CMD,
547 oper, err_str, err_rc);
551 if (token[0] == 'u' || token[0] == 'U')
552 id.pid = (strtoul(&token[1], &end, 0) |
553 (LNET_PID_USERFLAG));
555 id.pid = strtoul(token, &end, 0);
557 /* assuming '-' is part of hostname */
559 id.pid = LNET_PID_ANY;
560 id.nid = libcfs_str2nid(token);
561 if (id.nid == LNET_NID_ANY) {
562 snprintf(err_str, sizeof(err_str),
563 "\"cannot parse NID '%s'\"",
565 rc = LUSTRE_CFG_RC_BAD_PARAM;
566 cYAML_build_error(rc, seq_no, MANAGE_CMD,
572 id.nid = libcfs_str2nid(sep + 1);
573 if (id.nid == LNET_NID_ANY) {
574 snprintf(err_str, sizeof(err_str),
575 "\"cannot parse NID '%s'\"",
577 rc = LUSTRE_CFG_RC_BAD_PARAM;
578 cYAML_build_error(rc, seq_no, MANAGE_CMD,
585 LIBCFS_IOC_INIT_V2(ping, ping_hdr);
586 ping.ping_hdr.ioc_len = sizeof(ping);
588 ping.op_param = param;
589 ping.ping_count = LNET_INTERFACES_MAX_DEFAULT;
590 ping.ping_buf = data;
592 rc = l_ioctl(LNET_DEV_ID, ioc_call, &ping);
595 sizeof(err_str), "failed to %s %s: %s\n", oper,
596 id.pid == LNET_PID_ANY ?
597 libcfs_nid2str(id.nid) :
598 libcfs_id2str(id), strerror(errno));
599 rc = LUSTRE_CFG_RC_BAD_PARAM;
600 cYAML_build_error(rc, seq_no, MANAGE_CMD,
601 oper, err_str, err_rc);
605 if (cYAML_create_string(item, "primary nid",
606 libcfs_nid2str(ping.ping_id.nid)) == NULL)
609 if (cYAML_create_string(item, "Multi-Rail", ping.mr_info ?
610 "True" : "False") == NULL)
613 tmp = cYAML_create_seq(item, "peer ni");
617 for (i = 0; i < ping.ping_count; i++) {
618 if (!strcmp(libcfs_nid2str(ping.ping_buf[i].nid),
621 peer_ni = cYAML_create_seq_item(tmp);
624 memset(buf, 0, sizeof buf);
625 snprintf(buf, sizeof buf, "nid");
626 if (cYAML_create_string(peer_ni, buf,
627 libcfs_nid2str(ping.ping_buf[i].nid)) == NULL)
633 } while ((token = strtok(NULL, ",")) != NULL);
636 rc = LUSTRE_CFG_RC_NO_ERR;
641 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
642 cYAML_free_tree(root);
643 } else if (show_rc != NULL && *show_rc != NULL) {
644 struct cYAML *show_node;
645 show_node = cYAML_get_object_item(*show_rc, oper);
646 if (show_node != NULL && cYAML_is_sequence(show_node)) {
647 cYAML_insert_child(show_node, first_seq);
650 } else if (show_node == NULL) {
651 cYAML_insert_sibling((*show_rc)->cy_child,
655 cYAML_free_tree(root);
664 int lustre_lnet_ping_nid(char *ping_nids, int timeout, int seq_no,
665 struct cYAML **show_rc, struct cYAML **err_rc)
669 rc = infra_ping_nid(ping_nids, "ping", timeout, IOC_LIBCFS_PING_PEER,
670 seq_no, show_rc, err_rc);
674 int lustre_lnet_discover_nid(char *ping_nids, int force, int seq_no,
675 struct cYAML **show_rc, struct cYAML **err_rc)
679 rc = infra_ping_nid(ping_nids, "discover", force, IOC_LIBCFS_DISCOVER,
680 seq_no, show_rc, err_rc);
684 int lustre_lnet_config_peer_nid(char *pnid, char **nid, int num_nids,
685 bool mr, int seq_no, struct cYAML **err_rc)
687 struct lnet_ioctl_peer_cfg data;
688 lnet_nid_t prim_nid = LNET_NID_ANY;
689 int rc = LUSTRE_CFG_RC_NO_ERR;
691 bool nid0_used = false;
692 char err_str[LNET_MAX_STR_LEN] = {0};
693 lnet_nid_t *nids = allocate_create_nid_array(nid, num_nids, err_str);
696 prim_nid = libcfs_str2nid(pnid);
697 if (prim_nid == LNET_NID_ANY) {
698 snprintf(err_str, sizeof(err_str),
701 rc = LUSTRE_CFG_RC_MISSING_PARAM;
704 } else if (!nids || nids[0] == LNET_NID_ANY) {
705 snprintf(err_str, sizeof(err_str),
706 "no NIDs provided for configuration");
707 rc = LUSTRE_CFG_RC_MISSING_PARAM;
710 prim_nid = LNET_NID_ANY;
713 snprintf(err_str, sizeof(err_str), "\"Success\"");
715 LIBCFS_IOC_INIT_V2(data, prcfg_hdr);
719 * if prim_nid is not specified use the first nid in the list of
720 * nids provided as the prim_nid. NOTE: on entering 'if' we must
721 * have at least 1 NID
723 if (prim_nid == LNET_NID_ANY) {
728 /* Create the prim_nid first */
729 rc = dispatch_peer_ni_cmd(prim_nid, LNET_NID_ANY,
730 IOC_LIBCFS_ADD_PEER_NI,
731 &data, err_str, "add");
736 /* add the rest of the nids to the key nid if any are available */
737 for (idx = nid0_used ? 1 : 0 ; nids && idx < num_nids; idx++) {
739 * If prim_nid is not provided then the first nid in the
740 * list becomes the prim_nid. First time round the loop use
741 * LNET_NID_ANY for the first parameter, then use nid[0]
742 * as the key nid after wards
744 rc = dispatch_peer_ni_cmd(prim_nid, nids[idx],
745 IOC_LIBCFS_ADD_PEER_NI, &data,
755 cYAML_build_error(rc, seq_no, ADD_CMD, "peer_ni", err_str, err_rc);
759 int lustre_lnet_del_peer_nid(char *pnid, char **nid, int num_nids,
760 int seq_no, struct cYAML **err_rc)
762 struct lnet_ioctl_peer_cfg data;
764 int rc = LUSTRE_CFG_RC_NO_ERR;
766 char err_str[LNET_MAX_STR_LEN] = {0};
767 lnet_nid_t *nids = allocate_create_nid_array(nid, num_nids, err_str);
770 snprintf(err_str, sizeof(err_str),
771 "\"Primary nid is not provided\"");
772 rc = LUSTRE_CFG_RC_MISSING_PARAM;
775 prim_nid = libcfs_str2nid(pnid);
776 if (prim_nid == LNET_NID_ANY) {
777 rc = LUSTRE_CFG_RC_BAD_PARAM;
778 snprintf(err_str, sizeof(err_str),
785 snprintf(err_str, sizeof(err_str), "\"Success\"");
787 LIBCFS_IOC_INIT_V2(data, prcfg_hdr);
788 if (!nids || nids[0] == LNET_NID_ANY) {
789 rc = dispatch_peer_ni_cmd(prim_nid, LNET_NID_ANY,
790 IOC_LIBCFS_DEL_PEER_NI,
791 &data, err_str, "del");
795 for (idx = 0; nids && idx < num_nids; idx++) {
796 rc = dispatch_peer_ni_cmd(prim_nid, nids[idx],
797 IOC_LIBCFS_DEL_PEER_NI, &data,
807 cYAML_build_error(rc, seq_no, DEL_CMD, "peer_ni", err_str, err_rc);
811 int lustre_lnet_config_route(char *nw, char *gw, int hops, int prio,
812 int seq_no, struct cYAML **err_rc)
814 struct lnet_ioctl_config_data data;
815 lnet_nid_t gateway_nid;
816 int rc = LUSTRE_CFG_RC_NO_ERR;
817 __u32 net = LNET_NIDNET(LNET_NID_ANY);
818 char err_str[LNET_MAX_STR_LEN];
820 snprintf(err_str, sizeof(err_str), "\"Success\"");
822 if (nw == NULL || gw == NULL) {
825 "\"missing mandatory parameter in route config:'%s'\"",
826 (nw == NULL && gw == NULL) ? "network, gateway" :
827 (nw == NULL) ? "network" : "gateway");
828 rc = LUSTRE_CFG_RC_MISSING_PARAM;
832 net = libcfs_str2net(nw);
833 if (net == LNET_NIDNET(LNET_NID_ANY)) {
836 "\"cannot parse net %s\"", nw);
837 rc = LUSTRE_CFG_RC_BAD_PARAM;
841 gateway_nid = libcfs_str2nid(gw);
842 if (gateway_nid == LNET_NID_ANY) {
845 "\"cannot parse gateway NID '%s'\"", gw);
846 rc = LUSTRE_CFG_RC_BAD_PARAM;
851 /* hops is undefined */
852 hops = LNET_UNDEFINED_HOPS;
853 } else if (hops < 1 || hops > 255) {
856 "\"invalid hop count %d, must be between 1 and 255\"",
858 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
864 } else if (prio < 0) {
867 "\"invalid priority %d, must be greater than 0\"",
869 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
873 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
875 data.cfg_config_u.cfg_route.rtr_hop = hops;
876 data.cfg_config_u.cfg_route.rtr_priority = prio;
877 data.cfg_nid = gateway_nid;
879 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
884 "\"cannot add route: %s\"", strerror(errno));
889 cYAML_build_error(rc, seq_no, ADD_CMD, "route", err_str, err_rc);
894 int lustre_lnet_del_route(char *nw, char *gw,
895 int seq_no, struct cYAML **err_rc)
897 struct lnet_ioctl_config_data data;
898 lnet_nid_t gateway_nid;
899 int rc = LUSTRE_CFG_RC_NO_ERR;
900 __u32 net = LNET_NIDNET(LNET_NID_ANY);
901 char err_str[LNET_MAX_STR_LEN];
903 snprintf(err_str, sizeof(err_str), "\"Success\"");
905 if (nw == NULL || gw == NULL) {
908 "\"missing mandatory parameter in route delete: '%s'\"",
909 (nw == NULL && gw == NULL) ? "network, gateway" :
910 (nw == NULL) ? "network" : "gateway");
911 rc = LUSTRE_CFG_RC_MISSING_PARAM;
915 net = libcfs_str2net(nw);
916 if (net == LNET_NIDNET(LNET_NID_ANY)) {
919 "\"cannot parse net '%s'\"", nw);
920 rc = LUSTRE_CFG_RC_BAD_PARAM;
924 gateway_nid = libcfs_str2nid(gw);
925 if (gateway_nid == LNET_NID_ANY) {
928 "\"cannot parse gateway NID '%s'\"", gw);
929 rc = LUSTRE_CFG_RC_BAD_PARAM;
933 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
935 data.cfg_nid = gateway_nid;
937 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
942 "\"cannot delete route: %s\"", strerror(errno));
947 cYAML_build_error(rc, seq_no, DEL_CMD, "route", err_str, err_rc);
952 int lustre_lnet_show_route(char *nw, char *gw, int hops, int prio, int detail,
953 int seq_no, struct cYAML **show_rc,
954 struct cYAML **err_rc)
956 struct lnet_ioctl_config_data data;
957 lnet_nid_t gateway_nid;
958 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
960 __u32 net = LNET_NIDNET(LNET_NID_ANY);
962 struct cYAML *root = NULL, *route = NULL, *item = NULL;
963 struct cYAML *first_seq = NULL;
964 char err_str[LNET_MAX_STR_LEN];
967 snprintf(err_str, sizeof(err_str),
968 "\"out of memory\"");
971 net = libcfs_str2net(nw);
972 if (net == LNET_NIDNET(LNET_NID_ANY)) {
975 "\"cannot parse net '%s'\"", nw);
976 rc = LUSTRE_CFG_RC_BAD_PARAM;
981 /* show all routes without filtering on net */
982 net = LNET_NIDNET(LNET_NID_ANY);
986 gateway_nid = libcfs_str2nid(gw);
987 if (gateway_nid == LNET_NID_ANY) {
990 "\"cannot parse gateway NID '%s'\"", gw);
991 rc = LUSTRE_CFG_RC_BAD_PARAM;
995 /* show all routes with out filtering on gateway */
996 gateway_nid = LNET_NID_ANY;
998 if ((hops < 1 && hops != -1) || hops > 255) {
1001 "\"invalid hop count %d, must be between 0 and 256\"",
1003 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1007 /* create struct cYAML root object */
1008 root = cYAML_create_object(NULL, NULL);
1012 route = cYAML_create_seq(root, "route");
1017 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1020 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
1026 /* filter on provided data */
1027 if (net != LNET_NIDNET(LNET_NID_ANY) &&
1028 net != data.cfg_net)
1031 if (gateway_nid != LNET_NID_ANY &&
1032 gateway_nid != data.cfg_nid)
1036 hops != data.cfg_config_u.cfg_route.rtr_hop)
1040 prio != data.cfg_config_u.cfg_route.rtr_priority)
1043 /* default rc to -1 incase we hit the goto */
1047 item = cYAML_create_seq_item(route);
1051 if (first_seq == NULL)
1054 if (cYAML_create_string(item, "net",
1055 libcfs_net2str(data.cfg_net)) == NULL)
1058 if (cYAML_create_string(item, "gateway",
1059 libcfs_nid2str(data.cfg_nid)) == NULL)
1063 if (cYAML_create_number(item, "hop",
1064 (int) data.cfg_config_u.
1065 cfg_route.rtr_hop) ==
1069 if (cYAML_create_number(item, "priority",
1071 cfg_route.rtr_priority) == NULL)
1074 if (cYAML_create_string(item, "state",
1075 data.cfg_config_u.cfg_route.
1077 "up" : "down") == NULL)
1082 /* print output iff show_rc is not provided */
1083 if (show_rc == NULL)
1084 cYAML_print_tree(root);
1086 if (l_errno != ENOENT) {
1089 "\"cannot get routes: %s\"",
1094 rc = LUSTRE_CFG_RC_NO_ERR;
1096 snprintf(err_str, sizeof(err_str), "\"success\"");
1098 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
1099 cYAML_free_tree(root);
1100 } else if (show_rc != NULL && *show_rc != NULL) {
1101 struct cYAML *show_node;
1102 /* find the route node, if one doesn't exist then
1103 * insert one. Otherwise add to the one there
1105 show_node = cYAML_get_object_item(*show_rc, "route");
1106 if (show_node != NULL && cYAML_is_sequence(show_node)) {
1107 cYAML_insert_child(show_node, first_seq);
1110 } else if (show_node == NULL) {
1111 cYAML_insert_sibling((*show_rc)->cy_child,
1115 cYAML_free_tree(root);
1121 cYAML_build_error(rc, seq_no, SHOW_CMD, "route", err_str, err_rc);
1126 static int socket_intf_query(int request, char *intf,
1132 if (strlen(intf) >= IFNAMSIZ || ifr == NULL)
1133 return LUSTRE_CFG_RC_BAD_PARAM;
1135 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
1137 return LUSTRE_CFG_RC_BAD_PARAM;
1139 strcpy(ifr->ifr_name, intf);
1140 rc = ioctl(sockfd, request, ifr);
1142 rc = LUSTRE_CFG_RC_BAD_PARAM;
1150 * for each interface in the array of interfaces find the IP address of
1151 * that interface, create its nid and add it to an array of NIDs.
1152 * Stop if any of the interfaces is down
1154 static int lustre_lnet_intf2nids(struct lnet_dlc_network_descr *nw,
1155 lnet_nid_t **nids, __u32 *nnids)
1157 int i = 0, count = 0, rc;
1160 struct lnet_dlc_intf_descr *intf;
1162 if (nw == NULL || nids == NULL)
1163 return LUSTRE_CFG_RC_BAD_PARAM;
1165 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network)
1168 *nids = calloc(count, sizeof(lnet_nid_t));
1170 return LUSTRE_CFG_RC_OUT_OF_MEM;
1172 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network) {
1173 memset(&ifr, 0, sizeof(ifr));
1174 rc = socket_intf_query(SIOCGIFFLAGS, intf->intf_name, &ifr);
1178 if ((ifr.ifr_flags & IFF_UP) == 0) {
1179 rc = LUSTRE_CFG_RC_BAD_PARAM;
1183 memset(&ifr, 0, sizeof(ifr));
1184 rc = socket_intf_query(SIOCGIFADDR, intf->intf_name, &ifr);
1188 ip = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
1190 (*nids)[i] = LNET_MKNID(nw->nw_id, ip);
1205 * called repeatedly until a match or no more ip range
1207 * ip_range expression
1208 * interface list with all the interface names.
1209 * all the interfaces in the system.
1211 * try to match the ip_range expr to one of the interfaces' IPs in
1212 * the system. If we hit a patch for an interface. Check if that
1213 * interface name is in the list.
1215 * If there are more than one interface in the list, then make sure
1216 * that the IPs for all of these interfaces match the ip ranges
1219 * for each interface in intf_list
1220 * look up the intf name in ifa
1221 * if not there then no match
1222 * check ip obtained from ifa against a match to any of the
1224 * If no match, then fail
1226 * The result is that all the interfaces have to match.
1228 int lustre_lnet_match_ip_to_intf(struct ifaddrs *ifa,
1229 struct list_head *intf_list,
1230 struct list_head *ip_ranges)
1234 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1235 struct ifaddrs *ifaddr = ifa;
1236 struct lustre_lnet_ip_range_descr *ip_range;
1240 * if there are no explicit interfaces, and no ip ranges, then
1241 * configure the first tcp interface we encounter.
1243 if (list_empty(intf_list) && list_empty(ip_ranges)) {
1244 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1245 if (ifaddr->ifa_addr == NULL)
1248 if ((ifaddr->ifa_flags & IFF_UP) == 0)
1251 family = ifaddr->ifa_addr->sa_family;
1252 if (family == AF_INET &&
1253 strcmp(ifaddr->ifa_name, "lo") != 0) {
1254 rc = lustre_lnet_add_intf_descr
1255 (intf_list, ifaddr->ifa_name,
1256 strlen(ifaddr->ifa_name));
1258 if (rc != LUSTRE_CFG_RC_NO_ERR)
1261 return LUSTRE_CFG_RC_MATCH;
1264 return LUSTRE_CFG_RC_NO_MATCH;
1268 * First interface which matches an IP pattern will be used
1270 if (list_empty(intf_list)) {
1272 * no interfaces provided in the rule, but an ip range is
1273 * provided, so try and match an interface to the ip
1276 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1277 if (ifaddr->ifa_addr == NULL)
1280 if ((ifaddr->ifa_flags & IFF_UP) == 0)
1283 family = ifaddr->ifa_addr->sa_family;
1284 if (family == AF_INET) {
1285 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->
1288 list_for_each_entry(ip_range, ip_ranges,
1290 rc = cfs_ip_addr_match(bswap_32(ip),
1291 &ip_range->ipr_expr);
1295 rc = lustre_lnet_add_intf_descr
1296 (intf_list, ifaddr->ifa_name,
1297 strlen(ifaddr->ifa_name));
1299 if (rc != LUSTRE_CFG_RC_NO_ERR)
1305 if (!list_empty(intf_list))
1306 return LUSTRE_CFG_RC_MATCH;
1308 return LUSTRE_CFG_RC_NO_MATCH;
1312 * If an interface is explicitly specified the ip-range might or
1313 * might not be specified. if specified the interface needs to match the
1314 * ip-range. If no ip-range then the interfaces are
1315 * automatically matched if they are all up.
1316 * If > 1 interfaces all the interfaces must match for the NI to
1319 list_for_each_entry_safe(intf_descr, tmp, intf_list, intf_on_network) {
1320 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1321 if (ifaddr->ifa_addr == NULL)
1324 family = ifaddr->ifa_addr->sa_family;
1325 if (family == AF_INET &&
1326 strcmp(intf_descr->intf_name,
1327 ifaddr->ifa_name) == 0)
1331 if (ifaddr == NULL) {
1332 list_del(&intf_descr->intf_on_network);
1333 free_intf_descr(intf_descr);
1337 if ((ifaddr->ifa_flags & IFF_UP) == 0) {
1338 list_del(&intf_descr->intf_on_network);
1339 free_intf_descr(intf_descr);
1343 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr.s_addr;
1346 list_for_each_entry(ip_range, ip_ranges, ipr_entry) {
1347 rc = cfs_ip_addr_match(bswap_32(ip), &ip_range->ipr_expr);
1353 /* no match for this interface */
1354 list_del(&intf_descr->intf_on_network);
1355 free_intf_descr(intf_descr);
1359 return LUSTRE_CFG_RC_MATCH;
1362 int lustre_lnet_resolve_ip2nets_rule(struct lustre_lnet_ip2nets *ip2nets,
1363 lnet_nid_t **nids, __u32 *nnids)
1365 struct ifaddrs *ifa;
1366 int rc = LUSTRE_CFG_RC_NO_ERR;
1368 rc = getifaddrs(&ifa);
1372 rc = lustre_lnet_match_ip_to_intf(ifa,
1373 &ip2nets->ip2nets_net.nw_intflist,
1374 &ip2nets->ip2nets_ip_ranges);
1375 if (rc != LUSTRE_CFG_RC_MATCH) {
1380 rc = lustre_lnet_intf2nids(&ip2nets->ip2nets_net, nids, nnids);
1381 if (rc != LUSTRE_CFG_RC_NO_ERR) {
1392 lustre_lnet_ioctl_config_ni(struct list_head *intf_list,
1393 struct lnet_ioctl_config_lnd_tunables *tunables,
1394 struct cfs_expr_list *global_cpts,
1395 lnet_nid_t *nids, char *err_str)
1398 struct lnet_ioctl_config_ni *conf;
1399 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1400 int rc = LUSTRE_CFG_RC_NO_ERR, i = 0;
1403 struct lnet_dlc_intf_descr *intf_descr;
1405 struct cfs_expr_list *cpt_expr;
1407 list_for_each_entry(intf_descr, intf_list,
1409 if (tunables != NULL)
1410 len = sizeof(struct lnet_ioctl_config_ni) +
1411 sizeof(struct lnet_ioctl_config_lnd_tunables);
1413 len = sizeof(struct lnet_ioctl_config_ni);
1415 data = calloc(1, len);
1417 return LUSTRE_CFG_RC_OUT_OF_MEM;
1418 conf = (struct lnet_ioctl_config_ni*) data;
1419 if (tunables != NULL)
1420 tun = (struct lnet_ioctl_config_lnd_tunables*)
1423 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1424 conf->lic_cfg_hdr.ioc_len = len;
1425 conf->lic_nid = nids[i];
1426 strncpy(conf->lic_ni_intf[0], intf_descr->intf_name,
1429 if (intf_descr->cpt_expr != NULL)
1430 cpt_expr = intf_descr->cpt_expr;
1431 else if (global_cpts != NULL)
1432 cpt_expr = global_cpts;
1436 if (cpt_expr != NULL) {
1437 count = cfs_expr_list_values(cpt_expr,
1438 LNET_MAX_SHOW_NUM_CPT,
1441 memcpy(conf->lic_cpts, cpt_array,
1442 sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1451 conf->lic_ncpts = count;
1453 if (tunables != NULL)
1454 memcpy(tun, tunables, sizeof(*tunables));
1456 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1461 "\"cannot add network: %s\"", strerror(errno));
1469 return LUSTRE_CFG_RC_NO_ERR;
1473 lustre_lnet_config_ip2nets(struct lustre_lnet_ip2nets *ip2nets,
1474 struct lnet_ioctl_config_lnd_tunables *tunables,
1475 struct cfs_expr_list *global_cpts,
1476 int seq_no, struct cYAML **err_rc)
1478 lnet_nid_t *nids = NULL;
1481 char err_str[LNET_MAX_STR_LEN];
1483 snprintf(err_str, sizeof(err_str), "\"success\"");
1488 "\"incomplete ip2nets information\"");
1489 rc = LUSTRE_CFG_RC_BAD_PARAM;
1494 * call below function to resolve the rules into a list of nids.
1495 * The memory is allocated in that function then freed here when
1496 * it's no longer needed.
1498 rc = lustre_lnet_resolve_ip2nets_rule(ip2nets, &nids, &nnids);
1499 if (rc != LUSTRE_CFG_RC_NO_ERR && rc != LUSTRE_CFG_RC_MATCH) {
1502 "\"cannot resolve ip2nets rule\"");
1506 if (list_empty(&ip2nets->ip2nets_net.nw_intflist)) {
1507 snprintf(err_str, sizeof(err_str),
1508 "\"no interfaces match ip2nets rules\"");
1512 rc = lustre_lnet_ioctl_config_ni(&ip2nets->ip2nets_net.nw_intflist,
1513 tunables, global_cpts, nids,
1520 cYAML_build_error(rc, seq_no, ADD_CMD, "ip2nets", err_str, err_rc);
1524 int lustre_lnet_config_ni(struct lnet_dlc_network_descr *nw_descr,
1525 struct cfs_expr_list *global_cpts,
1527 struct lnet_ioctl_config_lnd_tunables *tunables,
1528 int seq_no, struct cYAML **err_rc)
1531 struct lnet_ioctl_config_ni *conf;
1532 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1533 char buf[LNET_MAX_STR_LEN];
1534 int rc = LUSTRE_CFG_RC_NO_ERR;
1535 char err_str[LNET_MAX_STR_LEN];
1536 lnet_nid_t *nids = NULL;
1540 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1543 snprintf(err_str, sizeof(err_str), "\"success\"");
1545 if (ip2net == NULL && (nw_descr == NULL || nw_descr->nw_id == 0 ||
1546 list_empty(&nw_descr->nw_intflist))) {
1549 "\"missing mandatory parameters in NI config: '%s'\"",
1550 (nw_descr == NULL) ? "network , interface" :
1551 (nw_descr->nw_id == 0) ? "network" : "interface");
1552 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1556 if (ip2net != NULL && strlen(ip2net) >= sizeof(buf)) {
1559 "\"ip2net string too long %d\"",
1560 (int)strlen(ip2net));
1561 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1565 if (ip2net != NULL) {
1566 if (tunables != NULL)
1567 len = sizeof(struct lnet_ioctl_config_ni) +
1568 sizeof(struct lnet_ioctl_config_lnd_tunables);
1570 len = sizeof(struct lnet_ioctl_config_ni);
1571 data = calloc(1, len);
1573 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1576 conf = (struct lnet_ioctl_config_ni*) data;
1577 if (tunables != NULL)
1578 tun = (struct lnet_ioctl_config_lnd_tunables*)
1579 (data + sizeof(*conf));
1581 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1582 conf->lic_cfg_hdr.ioc_len = len;
1583 strncpy(conf->lic_legacy_ip2nets, ip2net,
1586 if (global_cpts != NULL) {
1587 count = cfs_expr_list_values(global_cpts,
1588 LNET_MAX_SHOW_NUM_CPT,
1591 memcpy(conf->lic_cpts, cpt_array,
1592 sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1601 conf->lic_ncpts = count;
1603 if (tunables != NULL)
1604 memcpy(tun, tunables, sizeof(*tunables));
1606 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1611 "\"cannot add network: %s\"", strerror(errno));
1618 if (LNET_NETTYP(nw_descr->nw_id) == LOLND) {
1619 rc = LUSTRE_CFG_RC_NO_ERR;
1623 if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1626 "\"cannot parse net '%s'\"",
1627 libcfs_net2str(nw_descr->nw_id));
1628 rc = LUSTRE_CFG_RC_BAD_PARAM;
1632 if (list_empty(&nw_descr->nw_intflist)) {
1635 "\"no interface name provided\"");
1636 rc = LUSTRE_CFG_RC_BAD_PARAM;
1640 rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids);
1642 snprintf(err_str, sizeof(err_str),
1643 "\"bad parameter\"");
1644 rc = LUSTRE_CFG_RC_BAD_PARAM;
1648 rc = lustre_lnet_ioctl_config_ni(&nw_descr->nw_intflist,
1649 tunables, global_cpts, nids,
1653 if (nw_descr != NULL) {
1654 list_for_each_entry_safe(intf_descr, tmp,
1655 &nw_descr->nw_intflist,
1657 list_del(&intf_descr->intf_on_network);
1658 free_intf_descr(intf_descr);
1662 cYAML_build_error(rc, seq_no, ADD_CMD, "net", err_str, err_rc);
1673 int lustre_lnet_del_ni(struct lnet_dlc_network_descr *nw_descr,
1674 int seq_no, struct cYAML **err_rc)
1676 struct lnet_ioctl_config_ni data;
1677 int rc = LUSTRE_CFG_RC_NO_ERR, i;
1678 char err_str[LNET_MAX_STR_LEN];
1679 lnet_nid_t *nids = NULL;
1681 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1683 snprintf(err_str, sizeof(err_str), "\"success\"");
1685 if (nw_descr == NULL || nw_descr->nw_id == 0 ||
1686 list_empty(&nw_descr->nw_intflist)) {
1689 "\"missing mandatory parameter in deleting NI: '%s'\"",
1690 (nw_descr == NULL) ? "network , interface" :
1691 (nw_descr->nw_id == 0) ? "network" : "interface");
1692 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1696 if (LNET_NETTYP(nw_descr->nw_id) == LOLND)
1697 return LUSTRE_CFG_RC_NO_ERR;
1699 if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1702 "\"cannot parse net '%s'\"",
1703 libcfs_net2str(nw_descr->nw_id));
1704 rc = LUSTRE_CFG_RC_BAD_PARAM;
1708 rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids);
1710 snprintf(err_str, sizeof(err_str),
1711 "\"bad parameter\"");
1712 rc = LUSTRE_CFG_RC_BAD_PARAM;
1717 * no interfaces just the nw_id is specified
1720 nids = calloc(1, sizeof(*nids));
1722 snprintf(err_str, sizeof(err_str),
1723 "\"out of memory\"");
1724 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1727 nids[0] = LNET_MKNID(nw_descr->nw_id, 0);
1731 for (i = 0; i < nnids; i++) {
1732 LIBCFS_IOC_INIT_V2(data, lic_cfg_hdr);
1733 data.lic_nid = nids[i];
1735 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_LOCAL_NI, &data);
1740 "\"cannot del network: %s\"", strerror(errno));
1744 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
1746 list_del(&intf_descr->intf_on_network);
1747 free_intf_descr(intf_descr);
1751 cYAML_build_error(rc, seq_no, DEL_CMD, "net", err_str, err_rc);
1760 add_msg_stats_to_yaml_blk(struct cYAML *yaml,
1761 struct lnet_ioctl_comm_count *counts)
1763 if (cYAML_create_number(yaml, "put",
1764 counts->ico_put_count)
1767 if (cYAML_create_number(yaml, "get",
1768 counts->ico_get_count)
1771 if (cYAML_create_number(yaml, "reply",
1772 counts->ico_reply_count)
1775 if (cYAML_create_number(yaml, "ack",
1776 counts->ico_ack_count)
1779 if (cYAML_create_number(yaml, "hello",
1780 counts->ico_hello_count)
1787 static struct lnet_ioctl_comm_count *
1788 get_counts(struct lnet_ioctl_element_msg_stats *msg_stats, int idx)
1791 return &msg_stats->im_send_stats;
1793 return &msg_stats->im_recv_stats;
1795 return &msg_stats->im_drop_stats;
1800 int lustre_lnet_show_net(char *nw, int detail, int seq_no,
1801 struct cYAML **show_rc, struct cYAML **err_rc)
1804 struct lnet_ioctl_config_ni *ni_data;
1805 struct lnet_ioctl_config_lnd_tunables *lnd;
1806 struct lnet_ioctl_element_stats *stats;
1807 struct lnet_ioctl_element_msg_stats msg_stats;
1808 __u32 net = LNET_NIDNET(LNET_NID_ANY);
1809 __u32 prev_net = LNET_NIDNET(LNET_NID_ANY);
1810 int rc = LUSTRE_CFG_RC_OUT_OF_MEM, i, j;
1812 struct cYAML *root = NULL, *tunables = NULL,
1813 *net_node = NULL, *interfaces = NULL,
1814 *item = NULL, *first_seq = NULL,
1815 *tmp = NULL, *statistics = NULL;
1816 int str_buf_len = LNET_MAX_SHOW_NUM_CPT * 2;
1817 char str_buf[str_buf_len];
1819 char err_str[LNET_MAX_STR_LEN];
1820 bool exist = false, new_net = true;
1822 size_t buf_size = sizeof(*ni_data) + sizeof(*lnd) + sizeof(*stats);
1824 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1826 buf = calloc(1, buf_size);
1830 ni_data = (struct lnet_ioctl_config_ni *)buf;
1833 net = libcfs_str2net(nw);
1834 if (net == LNET_NIDNET(LNET_NID_ANY)) {
1837 "\"cannot parse net '%s'\"", nw);
1838 rc = LUSTRE_CFG_RC_BAD_PARAM;
1843 root = cYAML_create_object(NULL, NULL);
1847 net_node = cYAML_create_seq(root, "net");
1848 if (net_node == NULL)
1855 memset(buf, 0, buf_size);
1857 LIBCFS_IOC_INIT_V2(*ni_data, lic_cfg_hdr);
1859 * set the ioc_len to the proper value since INIT assumes
1862 ni_data->lic_cfg_hdr.ioc_len = buf_size;
1863 ni_data->lic_idx = i;
1865 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LOCAL_NI, ni_data);
1871 rc_net = LNET_NIDNET(ni_data->lic_nid);
1873 /* filter on provided data */
1874 if (net != LNET_NIDNET(LNET_NID_ANY) &&
1878 /* default rc to -1 in case we hit the goto */
1882 stats = (struct lnet_ioctl_element_stats *)ni_data->lic_bulk;
1883 lnd = (struct lnet_ioctl_config_lnd_tunables *)
1884 (ni_data->lic_bulk + sizeof(*stats));
1886 if (rc_net != prev_net) {
1893 if (!cYAML_create_string(net_node, "net type",
1894 libcfs_net2str(rc_net)))
1897 tmp = cYAML_create_seq(net_node, "local NI(s)");
1903 /* create the tree to be printed. */
1904 item = cYAML_create_seq_item(tmp);
1908 if (first_seq == NULL)
1911 if (cYAML_create_string(item, "nid",
1912 libcfs_nid2str(ni_data->lic_nid)) == NULL)
1915 if (cYAML_create_string(item,
1917 (ni_data->lic_status ==
1918 LNET_NI_STATUS_UP) ?
1919 "up" : "down") == NULL)
1922 /* don't add interfaces unless there is at least one
1924 if (strlen(ni_data->lic_ni_intf[0]) > 0) {
1925 interfaces = cYAML_create_object(item, "interfaces");
1926 if (interfaces == NULL)
1929 for (j = 0; j < LNET_INTERFACES_NUM; j++) {
1930 if (strlen(ni_data->lic_ni_intf[j]) > 0) {
1932 sizeof(str_buf), "%d", j);
1933 if (cYAML_create_string(interfaces,
1935 ni_data->lic_ni_intf[j]) ==
1946 statistics = cYAML_create_object(item, "statistics");
1947 if (statistics == NULL)
1950 if (cYAML_create_number(statistics, "send_count",
1951 stats->iel_send_count)
1955 if (cYAML_create_number(statistics, "recv_count",
1956 stats->iel_recv_count)
1960 if (cYAML_create_number(statistics, "drop_count",
1961 stats->iel_drop_count)
1966 goto continue_without_msg_stats;
1968 LIBCFS_IOC_INIT_V2(msg_stats, im_hdr);
1969 msg_stats.im_hdr.ioc_len = sizeof(msg_stats);
1970 msg_stats.im_idx = i;
1972 rc = l_ioctl(LNET_DEV_ID,
1973 IOC_LIBCFS_GET_LOCAL_NI_MSG_STATS,
1977 goto continue_without_msg_stats;
1980 for (k = 0; k < 3; k++) {
1981 struct lnet_ioctl_comm_count *counts;
1982 struct cYAML *msg_statistics = NULL;
1984 msg_statistics = cYAML_create_object(item,
1985 (char *)gmsg_stat_names[k]);
1986 if (msg_statistics == NULL)
1989 counts = get_counts(&msg_stats, k);
1993 if (!add_msg_stats_to_yaml_blk(msg_statistics,
1998 continue_without_msg_stats:
1999 tunables = cYAML_create_object(item, "tunables");
2003 rc = lustre_net_show_tunables(tunables, &lnd->lt_cmn);
2004 if (rc != LUSTRE_CFG_RC_NO_ERR)
2007 tunables = cYAML_create_object(item, "lnd tunables");
2008 if (tunables == NULL)
2011 rc = lustre_ni_show_tunables(tunables, LNET_NETTYP(rc_net),
2013 if (rc != LUSTRE_CFG_RC_NO_ERR)
2016 if (cYAML_create_number(item, "tcp bonding",
2017 ni_data->lic_tcp_bonding)
2021 if (cYAML_create_number(item, "dev cpt",
2022 ni_data->lic_dev_cpt) == NULL)
2025 /* out put the CPTs in the format: "[x,x,x,...]" */
2026 limit = str_buf + str_buf_len - 3;
2027 pos += snprintf(pos, limit - pos, "\"[");
2028 for (j = 0 ; ni_data->lic_ncpts >= 1 &&
2029 j < ni_data->lic_ncpts &&
2031 pos += snprintf(pos, limit - pos,
2032 "%d", ni_data->lic_cpts[j]);
2033 if ((j + 1) < ni_data->lic_ncpts)
2034 pos += snprintf(pos, limit - pos, ",");
2036 pos += snprintf(pos, 3, "]\"");
2038 if (ni_data->lic_ncpts >= 1 &&
2039 cYAML_create_string(item, "CPT",
2045 /* Print out the net information only if show_rc is not provided */
2046 if (show_rc == NULL)
2047 cYAML_print_tree(root);
2049 if (l_errno != ENOENT) {
2052 "\"cannot get networks: %s\"",
2057 rc = LUSTRE_CFG_RC_NO_ERR;
2059 snprintf(err_str, sizeof(err_str), "\"success\"");
2061 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2062 cYAML_free_tree(root);
2063 } else if (show_rc != NULL && *show_rc != NULL) {
2064 struct cYAML *show_node;
2065 /* find the net node, if one doesn't exist
2066 * then insert one. Otherwise add to the one there
2068 show_node = cYAML_get_object_item(*show_rc, "net");
2069 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2070 cYAML_insert_child(show_node, first_seq);
2073 } else if (show_node == NULL) {
2074 cYAML_insert_sibling((*show_rc)->cy_child,
2078 cYAML_free_tree(root);
2084 cYAML_build_error(rc, seq_no, SHOW_CMD, "net", err_str, err_rc);
2089 int lustre_lnet_enable_routing(int enable, int seq_no, struct cYAML **err_rc)
2091 struct lnet_ioctl_config_data data;
2092 int rc = LUSTRE_CFG_RC_NO_ERR;
2093 char err_str[LNET_MAX_STR_LEN];
2095 snprintf(err_str, sizeof(err_str), "\"success\"");
2097 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2098 data.cfg_config_u.cfg_buffers.buf_enable = (enable) ? 1 : 0;
2100 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIG_RTR, &data);
2105 "\"cannot %s routing %s\"",
2106 (enable) ? "enable" : "disable", strerror(errno));
2111 cYAML_build_error(rc, seq_no,
2112 (enable) ? ADD_CMD : DEL_CMD,
2113 "routing", err_str, err_rc);
2118 int ioctl_set_value(__u32 val, int ioc, char *name,
2119 int seq_no, struct cYAML **err_rc)
2121 struct lnet_ioctl_set_value data;
2122 int rc = LUSTRE_CFG_RC_NO_ERR;
2123 char err_str[LNET_MAX_STR_LEN];
2125 snprintf(err_str, sizeof(err_str), "\"success\"");
2127 LIBCFS_IOC_INIT_V2(data, sv_hdr);
2128 data.sv_value = val;
2130 rc = l_ioctl(LNET_DEV_ID, ioc , &data);
2135 "\"cannot configure %s to %d: %s\"", name,
2136 val, strerror(errno));
2139 cYAML_build_error(rc, seq_no, ADD_CMD, name, err_str, err_rc);
2144 int lustre_lnet_config_max_intf(int max, int seq_no, struct cYAML **err_rc)
2146 int rc = LUSTRE_CFG_RC_NO_ERR;
2147 char err_str[LNET_MAX_STR_LEN];
2148 char val[LNET_MAX_STR_LEN];
2150 snprintf(err_str, sizeof(err_str), "\"success\"");
2152 snprintf(val, sizeof(val), "%d", max);
2154 rc = write_sysfs_file(modparam_path, "lnet_interfaces_max", val,
2155 1, strlen(val) + 1);
2157 snprintf(err_str, sizeof(err_str),
2158 "\"cannot configure max interfaces: %s\"",
2161 cYAML_build_error(rc, seq_no, ADD_CMD, "max_interfaces", err_str, err_rc);
2166 int lustre_lnet_config_discovery(int enable, int seq_no, struct cYAML **err_rc)
2168 int rc = LUSTRE_CFG_RC_NO_ERR;
2169 char err_str[LNET_MAX_STR_LEN];
2170 char val[LNET_MAX_STR_LEN];
2172 snprintf(err_str, sizeof(err_str), "\"success\"");
2174 snprintf(val, sizeof(val), "%u", (enable) ? 0 : 1);
2176 rc = write_sysfs_file(modparam_path, "lnet_peer_discovery_disabled", val,
2177 1, strlen(val) + 1);
2179 snprintf(err_str, sizeof(err_str),
2180 "\"cannot configure discovery: %s\"",
2183 cYAML_build_error(rc, seq_no, ADD_CMD, "discovery", err_str, err_rc);
2189 int lustre_lnet_config_numa_range(int range, int seq_no, struct cYAML **err_rc)
2191 return ioctl_set_value(range, IOC_LIBCFS_SET_NUMA_RANGE,
2192 "numa_range", seq_no, err_rc);
2195 int lustre_lnet_config_buffers(int tiny, int small, int large, int seq_no,
2196 struct cYAML **err_rc)
2198 struct lnet_ioctl_config_data data;
2199 int rc = LUSTRE_CFG_RC_NO_ERR;
2200 char err_str[LNET_MAX_STR_LEN];
2202 snprintf(err_str, sizeof(err_str), "\"success\"");
2204 /* -1 indicates to ignore changes to this field */
2205 if (tiny < -1 || small < -1 || large < -1) {
2208 "\"tiny, small and large must be >= 0\"");
2209 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
2213 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2214 data.cfg_config_u.cfg_buffers.buf_tiny = tiny;
2215 data.cfg_config_u.cfg_buffers.buf_small = small;
2216 data.cfg_config_u.cfg_buffers.buf_large = large;
2218 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_BUF, &data);
2223 "\"cannot configure buffers: %s\"", strerror(errno));
2228 cYAML_build_error(rc, seq_no, ADD_CMD, "buf", err_str, err_rc);
2233 int lustre_lnet_show_routing(int seq_no, struct cYAML **show_rc,
2234 struct cYAML **err_rc)
2236 struct lnet_ioctl_config_data *data;
2237 struct lnet_ioctl_pool_cfg *pool_cfg = NULL;
2238 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2241 char *pools[LNET_NRBPOOLS] = {"tiny", "small", "large"};
2242 int buf_count[LNET_NRBPOOLS] = {0};
2243 struct cYAML *root = NULL, *pools_node = NULL,
2244 *type_node = NULL, *item = NULL, *cpt = NULL,
2245 *first_seq = NULL, *buffers = NULL;
2247 char err_str[LNET_MAX_STR_LEN];
2248 char node_name[LNET_MAX_STR_LEN];
2251 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2253 buf = calloc(1, sizeof(*data) + sizeof(*pool_cfg));
2257 data = (struct lnet_ioctl_config_data *)buf;
2259 root = cYAML_create_object(NULL, NULL);
2263 pools_node = cYAML_create_seq(root, "routing");
2264 if (pools_node == NULL)
2268 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
2269 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
2270 sizeof(struct lnet_ioctl_pool_cfg);
2271 data->cfg_count = i;
2273 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_BUF, data);
2281 pool_cfg = (struct lnet_ioctl_pool_cfg *)data->cfg_bulk;
2283 snprintf(node_name, sizeof(node_name), "cpt[%d]", i);
2284 item = cYAML_create_seq_item(pools_node);
2288 if (first_seq == NULL)
2291 cpt = cYAML_create_object(item, node_name);
2295 /* create the tree and print */
2296 for (j = 0; j < LNET_NRBPOOLS; j++) {
2297 type_node = cYAML_create_object(cpt, pools[j]);
2298 if (type_node == NULL)
2300 if (cYAML_create_number(type_node, "npages",
2301 pool_cfg->pl_pools[j].pl_npages)
2304 if (cYAML_create_number(type_node, "nbuffers",
2305 pool_cfg->pl_pools[j].
2306 pl_nbuffers) == NULL)
2308 if (cYAML_create_number(type_node, "credits",
2309 pool_cfg->pl_pools[j].
2310 pl_credits) == NULL)
2312 if (cYAML_create_number(type_node, "mincredits",
2313 pool_cfg->pl_pools[j].
2314 pl_mincredits) == NULL)
2316 /* keep track of the total count for each of the
2317 * tiny, small and large buffers */
2318 buf_count[j] += pool_cfg->pl_pools[j].pl_nbuffers;
2322 if (pool_cfg != NULL) {
2323 item = cYAML_create_seq_item(pools_node);
2327 if (cYAML_create_number(item, "enable", pool_cfg->pl_routing) ==
2332 /* create a buffers entry in the show. This is necessary so that
2333 * if the YAML output is used to configure a node, the buffer
2334 * configuration takes hold */
2335 buffers = cYAML_create_object(root, "buffers");
2336 if (buffers == NULL)
2339 for (i = 0; i < LNET_NRBPOOLS; i++) {
2340 if (cYAML_create_number(buffers, pools[i], buf_count[i]) == NULL)
2344 if (show_rc == NULL)
2345 cYAML_print_tree(root);
2347 if (l_errno != ENOENT) {
2350 "\"cannot get routing information: %s\"",
2355 rc = LUSTRE_CFG_RC_NO_ERR;
2357 snprintf(err_str, sizeof(err_str), "\"success\"");
2358 rc = LUSTRE_CFG_RC_NO_ERR;
2362 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2363 cYAML_free_tree(root);
2364 } else if (show_rc != NULL && *show_rc != NULL) {
2365 struct cYAML *routing_node;
2366 /* there should exist only one routing block and one
2367 * buffers block. If there already exists a previous one
2368 * then don't add another */
2369 routing_node = cYAML_get_object_item(*show_rc, "routing");
2370 if (routing_node == NULL) {
2371 cYAML_insert_sibling((*show_rc)->cy_child,
2375 cYAML_free_tree(root);
2381 cYAML_build_error(rc, seq_no, SHOW_CMD, "routing", err_str, err_rc);
2386 int lustre_lnet_show_peer(char *knid, int detail, int seq_no,
2387 struct cYAML **show_rc, struct cYAML **err_rc)
2390 * TODO: This function is changing in a future patch to accommodate
2391 * PEER_LIST and proper filtering on any nid of the peer
2393 struct lnet_ioctl_peer_cfg peer_info;
2394 struct lnet_peer_ni_credit_info *lpni_cri;
2395 struct lnet_ioctl_element_stats *lpni_stats;
2396 struct lnet_ioctl_element_msg_stats *msg_stats;
2398 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2403 struct cYAML *root = NULL, *peer = NULL, *peer_ni = NULL,
2404 *first_seq = NULL, *peer_root = NULL, *tmp = NULL,
2405 *msg_statistics = NULL, *statistics = NULL;
2406 char err_str[LNET_MAX_STR_LEN];
2407 struct lnet_process_id *list = NULL;
2411 snprintf(err_str, sizeof(err_str),
2412 "\"out of memory\"");
2414 /* create struct cYAML root object */
2415 root = cYAML_create_object(NULL, NULL);
2419 peer_root = cYAML_create_seq(root, "peer");
2420 if (peer_root == NULL)
2424 size = count * sizeof(struct lnet_process_id);
2425 list = malloc(size);
2431 list[0].nid = libcfs_str2nid(knid);
2435 memset(&peer_info, 0, sizeof(peer_info));
2436 LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2437 peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2438 peer_info.prcfg_size = size;
2439 peer_info.prcfg_bulk = list;
2442 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_LIST,
2444 count = peer_info.prcfg_count;
2448 if (l_errno != E2BIG) {
2451 "\"cannot get peer list: %s\"",
2457 size = peer_info.prcfg_size;
2458 list = malloc(size);
2467 data = malloc(size);
2472 for (i = 0; i < count; i++) {
2474 memset(&peer_info, 0, sizeof(peer_info));
2475 LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2476 peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2477 peer_info.prcfg_prim_nid = list[i].nid;
2478 peer_info.prcfg_size = size;
2479 peer_info.prcfg_bulk = data;
2482 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_NI,
2487 if (l_errno != E2BIG) {
2490 "\"cannot get peer information: %s\"",
2496 size = peer_info.prcfg_size;
2497 data = malloc(size);
2504 peer = cYAML_create_seq_item(peer_root);
2508 if (first_seq == NULL)
2511 lnet_nid_t pnid = peer_info.prcfg_prim_nid;
2512 if (cYAML_create_string(peer, "primary nid",
2513 libcfs_nid2str(pnid))
2516 if (cYAML_create_string(peer, "Multi-Rail",
2517 peer_info.prcfg_mr ? "True" : "False")
2521 * print out the state of the peer only if details are
2525 if (cYAML_create_number(peer, "peer state",
2526 peer_info.prcfg_state)
2531 tmp = cYAML_create_seq(peer, "peer ni");
2536 for (j = 0; j < peer_info.prcfg_count; j++) {
2538 lpni_cri = (void*)nidp + sizeof(nidp);
2539 lpni_stats = (void *)lpni_cri + sizeof(*lpni_cri);
2540 msg_stats = (void *)lpni_stats + sizeof(*lpni_stats);
2541 lpni_data = (void *)msg_stats + sizeof(*msg_stats);
2543 peer_ni = cYAML_create_seq_item(tmp);
2544 if (peer_ni == NULL)
2547 if (cYAML_create_string(peer_ni, "nid",
2548 libcfs_nid2str(*nidp))
2552 if (cYAML_create_string(peer_ni, "state",
2553 lpni_cri->cr_aliveness)
2560 if (cYAML_create_number(peer_ni, "max_ni_tx_credits",
2561 lpni_cri->cr_ni_peer_tx_credits)
2565 if (cYAML_create_number(peer_ni, "available_tx_credits",
2566 lpni_cri->cr_peer_tx_credits)
2570 if (cYAML_create_number(peer_ni, "min_tx_credits",
2571 lpni_cri->cr_peer_min_tx_credits)
2575 if (cYAML_create_number(peer_ni, "tx_q_num_of_buf",
2576 lpni_cri->cr_peer_tx_qnob)
2580 if (cYAML_create_number(peer_ni, "available_rtr_credits",
2581 lpni_cri->cr_peer_rtr_credits)
2585 if (cYAML_create_number(peer_ni, "min_rtr_credits",
2586 lpni_cri->cr_peer_min_rtr_credits)
2590 if (cYAML_create_number(peer_ni, "refcount",
2591 lpni_cri->cr_refcount) == NULL)
2594 statistics = cYAML_create_object(peer_ni, "statistics");
2595 if (statistics == NULL)
2598 if (cYAML_create_number(statistics, "send_count",
2599 lpni_stats->iel_send_count)
2603 if (cYAML_create_number(statistics, "recv_count",
2604 lpni_stats->iel_recv_count)
2608 if (cYAML_create_number(statistics, "drop_count",
2609 lpni_stats->iel_drop_count)
2616 for (k = 0; k < 3; k++) {
2617 struct lnet_ioctl_comm_count *counts;
2619 msg_statistics = cYAML_create_object(peer_ni,
2620 (char *) gmsg_stat_names[k]);
2621 if (msg_statistics == NULL)
2624 counts = get_counts(msg_stats, k);
2628 if (!add_msg_stats_to_yaml_blk(msg_statistics,
2636 /* print output iff show_rc is not provided */
2637 if (show_rc == NULL)
2638 cYAML_print_tree(root);
2640 snprintf(err_str, sizeof(err_str), "\"success\"");
2641 rc = LUSTRE_CFG_RC_NO_ERR;
2646 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2647 cYAML_free_tree(root);
2648 } else if (show_rc != NULL && *show_rc != NULL) {
2649 struct cYAML *show_node;
2650 /* find the peer node, if one doesn't exist then
2651 * insert one. Otherwise add to the one there
2653 show_node = cYAML_get_object_item(*show_rc,
2655 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2656 cYAML_insert_child(show_node, first_seq);
2659 } else if (show_node == NULL) {
2660 cYAML_insert_sibling((*show_rc)->cy_child,
2664 cYAML_free_tree(root);
2670 cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
2676 int lustre_lnet_list_peer(int seq_no,
2677 struct cYAML **show_rc, struct cYAML **err_rc)
2679 struct lnet_ioctl_peer_cfg peer_info;
2680 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2685 struct cYAML *root = NULL, *list_root = NULL, *first_seq = NULL;
2686 char err_str[LNET_MAX_STR_LEN];
2687 struct lnet_process_id *list = NULL;
2689 snprintf(err_str, sizeof(err_str),
2690 "\"out of memory\"");
2692 memset(&peer_info, 0, sizeof(peer_info));
2694 /* create struct cYAML root object */
2695 root = cYAML_create_object(NULL, NULL);
2699 list_root = cYAML_create_seq(root, "peer list");
2700 if (list_root == NULL)
2704 size = count * sizeof(struct lnet_process_id);
2705 list = malloc(size);
2711 LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2712 peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2713 peer_info.prcfg_size = size;
2714 peer_info.prcfg_bulk = list;
2717 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_LIST, &peer_info);
2718 count = peer_info.prcfg_count;
2722 if (l_errno != E2BIG) {
2725 "\"cannot get peer list: %s\"",
2731 size = peer_info.prcfg_size;
2732 list = malloc(size);
2739 /* count is now the actual number of ids in the list. */
2740 for (i = 0; i < count; i++) {
2741 if (cYAML_create_string(list_root, "nid",
2742 libcfs_nid2str(list[i].nid))
2747 /* print output iff show_rc is not provided */
2748 if (show_rc == NULL)
2749 cYAML_print_tree(root);
2751 snprintf(err_str, sizeof(err_str), "\"success\"");
2752 rc = LUSTRE_CFG_RC_NO_ERR;
2757 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2758 cYAML_free_tree(root);
2759 } else if (show_rc != NULL && *show_rc != NULL) {
2760 struct cYAML *show_node;
2761 /* find the peer node, if one doesn't exist then
2762 * insert one. Otherwise add to the one there
2764 show_node = cYAML_get_object_item(*show_rc,
2766 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2767 cYAML_insert_child(show_node, first_seq);
2770 } else if (show_node == NULL) {
2771 cYAML_insert_sibling((*show_rc)->cy_child,
2775 cYAML_free_tree(root);
2781 cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
2787 static void add_to_global(struct cYAML *show_rc, struct cYAML *node,
2790 struct cYAML *show_node;
2792 show_node = cYAML_get_object_item(show_rc, "global");
2793 if (show_node != NULL)
2794 cYAML_insert_sibling(show_node->cy_child,
2797 cYAML_insert_sibling(show_rc->cy_child,
2802 static int build_global_yaml_entry(char *err_str, int err_len, int seq_no,
2803 char *name, __u32 value,
2804 struct cYAML **show_rc,
2805 struct cYAML **err_rc, int err)
2807 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2808 struct cYAML *root = NULL, *global = NULL;
2815 root = cYAML_create_object(NULL, NULL);
2819 global = cYAML_create_object(root, "global");
2823 if (cYAML_create_number(global, name,
2827 if (show_rc == NULL)
2828 cYAML_print_tree(root);
2830 snprintf(err_str, err_len, "\"success\"");
2832 rc = LUSTRE_CFG_RC_NO_ERR;
2835 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2836 cYAML_free_tree(root);
2837 } else if (show_rc != NULL && *show_rc != NULL) {
2838 add_to_global(*show_rc, global, root);
2843 cYAML_build_error(rc, seq_no, SHOW_CMD, "global", err_str, err_rc);
2848 static int ioctl_show_global_values(int ioc, int seq_no, char *name,
2849 struct cYAML **show_rc,
2850 struct cYAML **err_rc)
2852 struct lnet_ioctl_set_value data;
2855 char err_str[LNET_MAX_STR_LEN];
2857 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2859 LIBCFS_IOC_INIT_V2(data, sv_hdr);
2861 rc = l_ioctl(LNET_DEV_ID, ioc, &data);
2866 "\"cannot get %s: %s\"",
2867 name, strerror(l_errno));
2870 return build_global_yaml_entry(err_str, sizeof(err_str), seq_no, name,
2871 data.sv_value, show_rc, err_rc, l_errno);
2874 int lustre_lnet_show_max_intf(int seq_no, struct cYAML **show_rc,
2875 struct cYAML **err_rc)
2877 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2878 char val[LNET_MAX_STR_LEN];
2879 int max_intf = -1, l_errno = 0;
2880 char err_str[LNET_MAX_STR_LEN];
2882 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2884 rc = read_sysfs_file(modparam_path, "lnet_interfaces_max", val,
2888 snprintf(err_str, sizeof(err_str),
2889 "\"cannot get max interfaces: %d\"", rc);
2891 max_intf = atoi(val);
2894 return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
2895 "max_intf", max_intf, show_rc,
2899 int lustre_lnet_show_discovery(int seq_no, struct cYAML **show_rc,
2900 struct cYAML **err_rc)
2902 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2903 char val[LNET_MAX_STR_LEN];
2904 int discovery = -1, l_errno = 0;
2905 char err_str[LNET_MAX_STR_LEN];
2907 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2909 rc = read_sysfs_file(modparam_path, "lnet_peer_discovery_disabled", val,
2913 snprintf(err_str, sizeof(err_str),
2914 "\"cannot get discovery setting: %d\"", rc);
2917 * The kernel stores a discovery disabled value. User space
2918 * shows whether discovery is enabled. So the value must be
2921 discovery = !atoi(val);
2924 return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
2925 "discovery", discovery, show_rc,
2929 int lustre_lnet_show_numa_range(int seq_no, struct cYAML **show_rc,
2930 struct cYAML **err_rc)
2932 return ioctl_show_global_values(IOC_LIBCFS_GET_NUMA_RANGE, seq_no,
2933 "numa_range", show_rc, err_rc);
2936 int lustre_lnet_show_stats(int seq_no, struct cYAML **show_rc,
2937 struct cYAML **err_rc)
2939 struct lnet_ioctl_lnet_stats data;
2942 char err_str[LNET_MAX_STR_LEN];
2943 struct cYAML *root = NULL, *stats = NULL;
2945 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2947 LIBCFS_IOC_INIT_V2(data, st_hdr);
2949 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LNET_STATS, &data);
2954 "\"cannot get lnet statistics: %s\"",
2960 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2962 root = cYAML_create_object(NULL, NULL);
2966 stats = cYAML_create_object(root, "statistics");
2970 if (cYAML_create_number(stats, "msgs_alloc",
2971 data.st_cntrs.msgs_alloc) == NULL)
2974 if (cYAML_create_number(stats, "msgs_max",
2975 data.st_cntrs.msgs_max) == NULL)
2978 if (cYAML_create_number(stats, "errors",
2979 data.st_cntrs.errors) == NULL)
2982 if (cYAML_create_number(stats, "send_count",
2983 data.st_cntrs.send_count) == NULL)
2986 if (cYAML_create_number(stats, "recv_count",
2987 data.st_cntrs.recv_count) == NULL)
2990 if (cYAML_create_number(stats, "route_count",
2991 data.st_cntrs.route_count) == NULL)
2994 if (cYAML_create_number(stats, "drop_count",
2995 data.st_cntrs.drop_count) == NULL)
2998 if (cYAML_create_number(stats, "send_length",
2999 data.st_cntrs.send_length) == NULL)
3002 if (cYAML_create_number(stats, "recv_length",
3003 data.st_cntrs.recv_length) == NULL)
3006 if (cYAML_create_number(stats, "route_length",
3007 data.st_cntrs.route_length) == NULL)
3010 if (cYAML_create_number(stats, "drop_length",
3011 data.st_cntrs.drop_length) == NULL)
3014 if (show_rc == NULL)
3015 cYAML_print_tree(root);
3017 snprintf(err_str, sizeof(err_str), "\"success\"");
3018 rc = LUSTRE_CFG_RC_NO_ERR;
3020 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
3021 cYAML_free_tree(root);
3022 } else if (show_rc != NULL && *show_rc != NULL) {
3023 cYAML_insert_sibling((*show_rc)->cy_child,
3030 cYAML_build_error(rc, seq_no, SHOW_CMD, "statistics", err_str, err_rc);
3035 typedef int (*cmd_handler_t)(struct cYAML *tree,
3036 struct cYAML **show_rc,
3037 struct cYAML **err_rc);
3039 static int handle_yaml_config_route(struct cYAML *tree, struct cYAML **show_rc,
3040 struct cYAML **err_rc)
3042 struct cYAML *net, *gw, *hop, *prio, *seq_no;
3044 net = cYAML_get_object_item(tree, "net");
3045 gw = cYAML_get_object_item(tree, "gateway");
3046 hop = cYAML_get_object_item(tree, "hop");
3047 prio = cYAML_get_object_item(tree, "priority");
3048 seq_no = cYAML_get_object_item(tree, "seq_no");
3050 return lustre_lnet_config_route((net) ? net->cy_valuestring : NULL,
3051 (gw) ? gw->cy_valuestring : NULL,
3052 (hop) ? hop->cy_valueint : -1,
3053 (prio) ? prio->cy_valueint : -1,
3054 (seq_no) ? seq_no->cy_valueint : -1,
3058 static void yaml_free_string_array(char **array, int num)
3061 char **sub_array = array;
3063 for (i = 0; i < num; i++) {
3064 if (*sub_array != NULL)
3074 * 0: <intf_name>['['<expr>']']
3075 * 1: <intf_name>['['<expr>']']
3077 static int yaml_copy_intf_info(struct cYAML *intf_tree,
3078 struct lnet_dlc_network_descr *nw_descr)
3080 struct cYAML *child = NULL;
3081 int intf_num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
3082 struct lnet_dlc_intf_descr *intf_descr, *tmp;
3084 if (intf_tree == NULL || nw_descr == NULL)
3085 return LUSTRE_CFG_RC_BAD_PARAM;
3087 /* now grab all the interfaces and their cpts */
3088 child = intf_tree->cy_child;
3089 while (child != NULL) {
3090 if (child->cy_valuestring == NULL) {
3091 child = child->cy_next;
3095 if (strlen(child->cy_valuestring) >= LNET_MAX_STR_LEN)
3098 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist,
3099 child->cy_valuestring,
3100 strlen(child->cy_valuestring));
3101 if (rc != LUSTRE_CFG_RC_NO_ERR)
3105 child = child->cy_next;
3109 return LUSTRE_CFG_RC_MISSING_PARAM;
3114 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
3116 list_del(&intf_descr->intf_on_network);
3117 free_intf_descr(intf_descr);
3124 yaml_extract_cmn_tunables(struct cYAML *tree,
3125 struct lnet_ioctl_config_lnd_cmn_tunables *tunables,
3126 struct cfs_expr_list **global_cpts)
3128 struct cYAML *tun, *item, *smp;
3131 tun = cYAML_get_object_item(tree, "tunables");
3133 item = cYAML_get_object_item(tun, "peer_timeout");
3135 tunables->lct_peer_timeout = item->cy_valueint;
3136 item = cYAML_get_object_item(tun, "peer_credits");
3138 tunables->lct_peer_tx_credits = item->cy_valueint;
3139 item = cYAML_get_object_item(tun, "peer_buffer_credits");
3141 tunables->lct_peer_rtr_credits = item->cy_valueint;
3142 item = cYAML_get_object_item(tun, "credits");
3144 tunables->lct_max_tx_credits = item->cy_valueint;
3145 smp = cYAML_get_object_item(tun, "CPT");
3147 rc = cfs_expr_list_parse(smp->cy_valuestring,
3148 strlen(smp->cy_valuestring),
3149 0, UINT_MAX, global_cpts);
3151 *global_cpts = NULL;
3161 yaml_extract_tunables(struct cYAML *tree,
3162 struct lnet_ioctl_config_lnd_tunables *tunables,
3163 struct cfs_expr_list **global_cpts,
3168 rc = yaml_extract_cmn_tunables(tree, &tunables->lt_cmn,
3174 lustre_yaml_extract_lnd_tunables(tree, net_type,
3182 * - net type: <net>[<NUM>]
3184 * - nid: <ip>@<net>[<NUM>]
3187 * 0: <intf_name>['['<expr>']']
3188 * 1: <intf_name>['['<expr>']']
3190 * peer_timeout: <NUM>
3191 * peer_credits: <NUM>
3192 * peer_buffer_credits: <NUM>
3195 * peercredits_hiw: <NUM>
3196 * map_on_demand: <NUM>
3197 * concurrent_sends: <NUM>
3198 * fmr_pool_size: <NUM>
3199 * fmr_flush_trigger: <NUM>
3202 * At least one interface is required. If no interfaces are provided the
3203 * network interface can not be configured.
3205 static int handle_yaml_config_ni(struct cYAML *tree, struct cYAML **show_rc,
3206 struct cYAML **err_rc)
3208 struct cYAML *net, *intf, *seq_no, *ip2net = NULL, *local_nis = NULL,
3210 int num_entries = 0, rc;
3211 struct lnet_dlc_network_descr nw_descr;
3212 struct cfs_expr_list *global_cpts = NULL;
3213 struct lnet_ioctl_config_lnd_tunables tunables;
3216 memset(&tunables, 0, sizeof(tunables));
3218 INIT_LIST_HEAD(&nw_descr.network_on_rule);
3219 INIT_LIST_HEAD(&nw_descr.nw_intflist);
3221 ip2net = cYAML_get_object_item(tree, "ip2net");
3222 net = cYAML_get_object_item(tree, "net type");
3224 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
3226 nw_descr.nw_id = LOLND;
3229 * if neither net nor ip2nets are present, then we can not
3230 * configure the network.
3232 if (!net && !ip2net)
3233 return LUSTRE_CFG_RC_MISSING_PARAM;
3235 local_nis = cYAML_get_object_item(tree, "local NI(s)");
3236 if (local_nis == NULL)
3237 return LUSTRE_CFG_RC_MISSING_PARAM;
3239 if (!cYAML_is_sequence(local_nis))
3240 return LUSTRE_CFG_RC_BAD_PARAM;
3242 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
3243 intf = cYAML_get_object_item(item, "interfaces");
3246 num_entries = yaml_copy_intf_info(intf, &nw_descr);
3247 if (num_entries <= 0) {
3248 cYAML_build_error(num_entries, -1, "ni", "add",
3249 "bad interface list",
3251 return LUSTRE_CFG_RC_BAD_PARAM;
3255 found = yaml_extract_tunables(tree, &tunables, &global_cpts,
3256 LNET_NETTYP(nw_descr.nw_id));
3257 seq_no = cYAML_get_object_item(tree, "seq_no");
3259 rc = lustre_lnet_config_ni(&nw_descr,
3261 (ip2net) ? ip2net->cy_valuestring : NULL,
3262 (found) ? &tunables: NULL,
3263 (seq_no) ? seq_no->cy_valueint : -1,
3266 if (global_cpts != NULL)
3267 cfs_expr_list_free(global_cpts);
3274 * - net-spec: <tcp|o2ib|gni>[NUM]
3276 * 0: <intf name>['['<expr>']']
3277 * 1: <intf name>['['<expr>']']
3279 * 0: <expr.expr.expr.expr>
3280 * 1: <expr.expr.expr.expr>
3282 static int handle_yaml_config_ip2nets(struct cYAML *tree,
3283 struct cYAML **show_rc,
3284 struct cYAML **err_rc)
3286 struct cYAML *net, *ip_range, *item = NULL, *intf = NULL,
3288 struct lustre_lnet_ip2nets ip2nets;
3289 struct lustre_lnet_ip_range_descr *ip_range_descr = NULL,
3291 int rc = LUSTRE_CFG_RC_NO_ERR;
3292 struct cfs_expr_list *global_cpts = NULL;
3293 struct cfs_expr_list *el, *el_tmp;
3294 struct lnet_ioctl_config_lnd_tunables tunables;
3295 struct lnet_dlc_intf_descr *intf_descr, *intf_tmp;
3298 memset(&tunables, 0, sizeof(tunables));
3300 /* initialize all lists */
3301 INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
3302 INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
3303 INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
3305 net = cYAML_get_object_item(tree, "net-spec");
3307 return LUSTRE_CFG_RC_BAD_PARAM;
3309 if (net != NULL && net->cy_valuestring == NULL)
3310 return LUSTRE_CFG_RC_BAD_PARAM;
3312 /* assign the network id */
3313 ip2nets.ip2nets_net.nw_id = libcfs_str2net(net->cy_valuestring);
3314 if (ip2nets.ip2nets_net.nw_id == LNET_NID_ANY)
3315 return LUSTRE_CFG_RC_BAD_PARAM;
3317 seq_no = cYAML_get_object_item(tree, "seq_no");
3319 intf = cYAML_get_object_item(tree, "interfaces");
3321 rc = yaml_copy_intf_info(intf, &ip2nets.ip2nets_net);
3323 return LUSTRE_CFG_RC_BAD_PARAM;
3326 ip_range = cYAML_get_object_item(tree, "ip-range");
3327 if (ip_range != NULL) {
3328 item = ip_range->cy_child;
3329 while (item != NULL) {
3330 if (item->cy_valuestring == NULL) {
3331 item = item->cy_next;
3335 rc = lustre_lnet_add_ip_range(&ip2nets.ip2nets_ip_ranges,
3336 item->cy_valuestring);
3338 if (rc != LUSTRE_CFG_RC_NO_ERR)
3341 item = item->cy_next;
3345 found = yaml_extract_tunables(tree, &tunables, &global_cpts,
3346 LNET_NETTYP(ip2nets.ip2nets_net.nw_id));
3348 rc = lustre_lnet_config_ip2nets(&ip2nets,
3349 (found) ? &tunables : NULL,
3351 (seq_no) ? seq_no->cy_valueint : -1,
3355 * don't stop because there was no match. Continue processing the
3356 * rest of the rules. If non-match then nothing is configured
3358 if (rc == LUSTRE_CFG_RC_NO_MATCH)
3359 rc = LUSTRE_CFG_RC_NO_ERR;
3361 list_for_each_entry_safe(intf_descr, intf_tmp,
3362 &ip2nets.ip2nets_net.nw_intflist,
3364 list_del(&intf_descr->intf_on_network);
3365 free_intf_descr(intf_descr);
3368 list_for_each_entry_safe(ip_range_descr, tmp,
3369 &ip2nets.ip2nets_ip_ranges,
3371 list_del(&ip_range_descr->ipr_entry);
3372 list_for_each_entry_safe(el, el_tmp, &ip_range_descr->ipr_expr,
3374 list_del(&el->el_link);
3375 cfs_expr_list_free(el);
3377 free(ip_range_descr);
3383 static int handle_yaml_del_ni(struct cYAML *tree, struct cYAML **show_rc,
3384 struct cYAML **err_rc)
3386 struct cYAML *net = NULL, *intf = NULL, *seq_no = NULL, *item = NULL,
3388 int num_entries, rc;
3389 struct lnet_dlc_network_descr nw_descr;
3391 INIT_LIST_HEAD(&nw_descr.network_on_rule);
3392 INIT_LIST_HEAD(&nw_descr.nw_intflist);
3394 net = cYAML_get_object_item(tree, "net type");
3396 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
3398 local_nis = cYAML_get_object_item(tree, "local NI(s)");
3399 if (local_nis == NULL)
3400 return LUSTRE_CFG_RC_MISSING_PARAM;
3402 if (!cYAML_is_sequence(local_nis))
3403 return LUSTRE_CFG_RC_BAD_PARAM;
3405 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
3406 intf = cYAML_get_object_item(item, "interfaces");
3409 num_entries = yaml_copy_intf_info(intf, &nw_descr);
3410 if (num_entries <= 0) {
3411 cYAML_build_error(num_entries, -1, "ni", "add",
3412 "bad interface list",
3414 return LUSTRE_CFG_RC_BAD_PARAM;
3418 seq_no = cYAML_get_object_item(tree, "seq_no");
3420 rc = lustre_lnet_del_ni((net) ? &nw_descr : NULL,
3421 (seq_no) ? seq_no->cy_valueint : -1,
3427 static int yaml_copy_peer_nids(struct cYAML *tree, char ***nidsppp, bool del)
3429 struct cYAML *nids_entry = NULL, *child = NULL, *entry = NULL,
3432 int num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
3434 prim_nid = cYAML_get_object_item(tree, "primary nid");
3435 if (!prim_nid || !prim_nid->cy_valuestring)
3436 return LUSTRE_CFG_RC_MISSING_PARAM;
3438 nids_entry = cYAML_get_object_item(tree, "peer ni");
3439 if (cYAML_is_sequence(nids_entry)) {
3440 while (cYAML_get_next_seq_item(nids_entry, &child)) {
3441 entry = cYAML_get_object_item(child, "nid");
3442 /* don't count an empty entry */
3443 if (!entry || !entry->cy_valuestring)
3446 if ((strcmp(entry->cy_valuestring, prim_nid->cy_valuestring)
3449 * primary nid is present in the list of
3450 * nids so that means we want to delete
3451 * the entire peer, so no need to go
3452 * further. Just delete the entire peer.
3462 return LUSTRE_CFG_RC_MISSING_PARAM;
3464 nids = calloc(sizeof(*nids) * num, 1);
3466 return LUSTRE_CFG_RC_OUT_OF_MEM;
3468 /* now grab all the nids */
3471 while (cYAML_get_next_seq_item(nids_entry, &child)) {
3472 entry = cYAML_get_object_item(child, "nid");
3473 if (!entry || !entry->cy_valuestring)
3476 nids[num] = calloc(strlen(entry->cy_valuestring) + 1, 1);
3478 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3481 strncpy(nids[num], entry->cy_valuestring,
3482 strlen(entry->cy_valuestring));
3492 yaml_free_string_array(nids, num);
3497 static int handle_yaml_config_peer(struct cYAML *tree, struct cYAML **show_rc,
3498 struct cYAML **err_rc)
3502 struct cYAML *seq_no, *prim_nid, *non_mr;
3504 num = yaml_copy_peer_nids(tree, &nids, false);
3508 seq_no = cYAML_get_object_item(tree, "seq_no");
3509 prim_nid = cYAML_get_object_item(tree, "primary nid");
3510 non_mr = cYAML_get_object_item(tree, "non_mr");
3512 rc = lustre_lnet_config_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
3514 (non_mr) ? false : true,
3515 (seq_no) ? seq_no->cy_valueint : -1,
3518 yaml_free_string_array(nids, num);
3522 static int handle_yaml_del_peer(struct cYAML *tree, struct cYAML **show_rc,
3523 struct cYAML **err_rc)
3527 struct cYAML *seq_no, *prim_nid;
3529 num = yaml_copy_peer_nids(tree, &nids, true);
3533 seq_no = cYAML_get_object_item(tree, "seq_no");
3534 prim_nid = cYAML_get_object_item(tree, "primary nid");
3536 rc = lustre_lnet_del_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
3538 (seq_no) ? seq_no->cy_valueint : -1,
3541 yaml_free_string_array(nids, num);
3545 static int handle_yaml_config_buffers(struct cYAML *tree,
3546 struct cYAML **show_rc,
3547 struct cYAML **err_rc)
3550 struct cYAML *tiny, *small, *large, *seq_no;
3552 tiny = cYAML_get_object_item(tree, "tiny");
3553 small = cYAML_get_object_item(tree, "small");
3554 large = cYAML_get_object_item(tree, "large");
3555 seq_no = cYAML_get_object_item(tree, "seq_no");
3557 rc = lustre_lnet_config_buffers((tiny) ? tiny->cy_valueint : -1,
3558 (small) ? small->cy_valueint : -1,
3559 (large) ? large->cy_valueint : -1,
3560 (seq_no) ? seq_no->cy_valueint : -1,
3566 static int handle_yaml_config_routing(struct cYAML *tree,
3567 struct cYAML **show_rc,
3568 struct cYAML **err_rc)
3570 int rc = LUSTRE_CFG_RC_NO_ERR;
3571 struct cYAML *seq_no, *enable;
3573 seq_no = cYAML_get_object_item(tree, "seq_no");
3574 enable = cYAML_get_object_item(tree, "enable");
3577 rc = lustre_lnet_enable_routing(enable->cy_valueint,
3579 seq_no->cy_valueint : -1,
3586 static int handle_yaml_del_route(struct cYAML *tree, struct cYAML **show_rc,
3587 struct cYAML **err_rc)
3591 struct cYAML *seq_no;
3593 net = cYAML_get_object_item(tree, "net");
3594 gw = cYAML_get_object_item(tree, "gateway");
3595 seq_no = cYAML_get_object_item(tree, "seq_no");
3597 return lustre_lnet_del_route((net) ? net->cy_valuestring : NULL,
3598 (gw) ? gw->cy_valuestring : NULL,
3599 (seq_no) ? seq_no->cy_valueint : -1,
3603 static int handle_yaml_del_routing(struct cYAML *tree, struct cYAML **show_rc,
3604 struct cYAML **err_rc)
3606 struct cYAML *seq_no;
3608 seq_no = cYAML_get_object_item(tree, "seq_no");
3610 return lustre_lnet_enable_routing(0, (seq_no) ?
3611 seq_no->cy_valueint : -1,
3615 static int handle_yaml_show_route(struct cYAML *tree, struct cYAML **show_rc,
3616 struct cYAML **err_rc)
3622 struct cYAML *detail;
3623 struct cYAML *seq_no;
3625 net = cYAML_get_object_item(tree, "net");
3626 gw = cYAML_get_object_item(tree, "gateway");
3627 hop = cYAML_get_object_item(tree, "hop");
3628 prio = cYAML_get_object_item(tree, "priority");
3629 detail = cYAML_get_object_item(tree, "detail");
3630 seq_no = cYAML_get_object_item(tree, "seq_no");
3632 return lustre_lnet_show_route((net) ? net->cy_valuestring : NULL,
3633 (gw) ? gw->cy_valuestring : NULL,
3634 (hop) ? hop->cy_valueint : -1,
3635 (prio) ? prio->cy_valueint : -1,
3636 (detail) ? detail->cy_valueint : 0,
3637 (seq_no) ? seq_no->cy_valueint : -1,
3642 static int handle_yaml_show_net(struct cYAML *tree, struct cYAML **show_rc,
3643 struct cYAML **err_rc)
3645 struct cYAML *net, *detail, *seq_no;
3647 net = cYAML_get_object_item(tree, "net");
3648 detail = cYAML_get_object_item(tree, "detail");
3649 seq_no = cYAML_get_object_item(tree, "seq_no");
3651 return lustre_lnet_show_net((net) ? net->cy_valuestring : NULL,
3652 (detail) ? detail->cy_valueint : 0,
3653 (seq_no) ? seq_no->cy_valueint : -1,
3658 static int handle_yaml_show_routing(struct cYAML *tree, struct cYAML **show_rc,
3659 struct cYAML **err_rc)
3661 struct cYAML *seq_no;
3663 seq_no = cYAML_get_object_item(tree, "seq_no");
3665 return lustre_lnet_show_routing((seq_no) ? seq_no->cy_valueint : -1,
3669 static int handle_yaml_show_peers(struct cYAML *tree, struct cYAML **show_rc,
3670 struct cYAML **err_rc)
3672 struct cYAML *seq_no, *nid, *detail;
3674 seq_no = cYAML_get_object_item(tree, "seq_no");
3675 detail = cYAML_get_object_item(tree, "detail");
3676 nid = cYAML_get_object_item(tree, "nid");
3678 return lustre_lnet_show_peer((nid) ? nid->cy_valuestring : NULL,
3679 (detail) ? detail->cy_valueint : 0,
3680 (seq_no) ? seq_no->cy_valueint : -1,
3684 static int handle_yaml_show_stats(struct cYAML *tree, struct cYAML **show_rc,
3685 struct cYAML **err_rc)
3687 struct cYAML *seq_no;
3689 seq_no = cYAML_get_object_item(tree, "seq_no");
3691 return lustre_lnet_show_stats((seq_no) ? seq_no->cy_valueint : -1,
3695 static int handle_yaml_config_numa(struct cYAML *tree, struct cYAML **show_rc,
3696 struct cYAML **err_rc)
3698 struct cYAML *seq_no, *range;
3700 seq_no = cYAML_get_object_item(tree, "seq_no");
3701 range = cYAML_get_object_item(tree, "range");
3703 return lustre_lnet_config_numa_range(range ? range->cy_valueint : -1,
3704 seq_no ? seq_no->cy_valueint : -1,
3708 static int handle_yaml_del_numa(struct cYAML *tree, struct cYAML **show_rc,
3709 struct cYAML **err_rc)
3711 struct cYAML *seq_no;
3713 seq_no = cYAML_get_object_item(tree, "seq_no");
3715 return lustre_lnet_config_numa_range(0, seq_no ? seq_no->cy_valueint : -1,
3719 static int handle_yaml_show_numa(struct cYAML *tree, struct cYAML **show_rc,
3720 struct cYAML **err_rc)
3722 struct cYAML *seq_no;
3724 seq_no = cYAML_get_object_item(tree, "seq_no");
3726 return lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint : -1,
3730 static int handle_yaml_config_global_settings(struct cYAML *tree,
3731 struct cYAML **show_rc,
3732 struct cYAML **err_rc)
3734 struct cYAML *max_intf, *numa, *discovery, *seq_no;
3737 seq_no = cYAML_get_object_item(tree, "seq_no");
3738 max_intf = cYAML_get_object_item(tree, "max_intf");
3740 rc = lustre_lnet_config_max_intf(max_intf->cy_valueint,
3741 seq_no ? seq_no->cy_valueint
3745 numa = cYAML_get_object_item(tree, "numa_range");
3747 rc = lustre_lnet_config_numa_range(numa->cy_valueint,
3748 seq_no ? seq_no->cy_valueint
3752 discovery = cYAML_get_object_item(tree, "discovery");
3754 rc = lustre_lnet_config_discovery(discovery->cy_valueint,
3755 seq_no ? seq_no->cy_valueint
3762 static int handle_yaml_del_global_settings(struct cYAML *tree,
3763 struct cYAML **show_rc,
3764 struct cYAML **err_rc)
3766 struct cYAML *max_intf, *numa, *discovery, *seq_no;
3769 seq_no = cYAML_get_object_item(tree, "seq_no");
3770 max_intf = cYAML_get_object_item(tree, "max_intf");
3772 rc = lustre_lnet_config_max_intf(LNET_INTERFACES_MAX_DEFAULT,
3773 seq_no ? seq_no->cy_valueint
3777 numa = cYAML_get_object_item(tree, "numa_range");
3779 rc = lustre_lnet_config_numa_range(0,
3780 seq_no ? seq_no->cy_valueint
3784 /* peer discovery is enabled by default */
3785 discovery = cYAML_get_object_item(tree, "discovery");
3787 rc = lustre_lnet_config_discovery(1,
3788 seq_no ? seq_no->cy_valueint
3795 static int handle_yaml_show_global_settings(struct cYAML *tree,
3796 struct cYAML **show_rc,
3797 struct cYAML **err_rc)
3799 struct cYAML *max_intf, *numa, *discovery, *seq_no;
3802 seq_no = cYAML_get_object_item(tree, "seq_no");
3803 max_intf = cYAML_get_object_item(tree, "max_intf");
3805 rc = lustre_lnet_show_max_intf(seq_no ? seq_no->cy_valueint
3809 numa = cYAML_get_object_item(tree, "numa_range");
3811 rc = lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint
3815 discovery = cYAML_get_object_item(tree, "discovery");
3817 rc = lustre_lnet_show_discovery(seq_no ? seq_no->cy_valueint
3824 static int handle_yaml_ping(struct cYAML *tree, struct cYAML **show_rc,
3825 struct cYAML **err_rc)
3827 struct cYAML *seq_no, *nid, *timeout;
3829 seq_no = cYAML_get_object_item(tree, "seq_no");
3830 nid = cYAML_get_object_item(tree, "primary nid");
3831 timeout = cYAML_get_object_item(tree, "timeout");
3833 return lustre_lnet_ping_nid((nid) ? nid->cy_valuestring : NULL,
3834 (timeout) ? timeout->cy_valueint : 1000,
3835 (seq_no) ? seq_no->cy_valueint : -1,
3839 static int handle_yaml_discover(struct cYAML *tree, struct cYAML **show_rc,
3840 struct cYAML **err_rc)
3842 struct cYAML *seq_no, *nid, *force;
3844 seq_no = cYAML_get_object_item(tree, "seq_no");
3845 nid = cYAML_get_object_item(tree, "primary nid");
3846 force = cYAML_get_object_item(tree, "force");
3848 return lustre_lnet_discover_nid((nid) ? nid->cy_valuestring : NULL,
3849 (force) ? force->cy_valueint : 0,
3850 (seq_no) ? seq_no->cy_valueint : -1,
3854 static int handle_yaml_no_op()
3856 return LUSTRE_CFG_RC_NO_ERR;
3859 struct lookup_cmd_hdlr_tbl {
3864 static struct lookup_cmd_hdlr_tbl lookup_config_tbl[] = {
3865 { .name = "route", .cb = handle_yaml_config_route },
3866 { .name = "net", .cb = handle_yaml_config_ni },
3867 { .name = "ip2nets", .cb = handle_yaml_config_ip2nets },
3868 { .name = "peer", .cb = handle_yaml_config_peer },
3869 { .name = "routing", .cb = handle_yaml_config_routing },
3870 { .name = "buffers", .cb = handle_yaml_config_buffers },
3871 { .name = "statistics", .cb = handle_yaml_no_op },
3872 { .name = "global", .cb = handle_yaml_config_global_settings},
3873 { .name = "numa", .cb = handle_yaml_config_numa },
3874 { .name = "ping", .cb = handle_yaml_no_op },
3875 { .name = "discover", .cb = handle_yaml_no_op },
3878 static struct lookup_cmd_hdlr_tbl lookup_del_tbl[] = {
3879 { .name = "route", .cb = handle_yaml_del_route },
3880 { .name = "net", .cb = handle_yaml_del_ni },
3881 { .name = "ip2nets", .cb = handle_yaml_no_op },
3882 { .name = "peer", .cb = handle_yaml_del_peer },
3883 { .name = "routing", .cb = handle_yaml_del_routing },
3884 { .name = "buffers", .cb = handle_yaml_no_op },
3885 { .name = "statistics", .cb = handle_yaml_no_op },
3886 { .name = "global", .cb = handle_yaml_del_global_settings},
3887 { .name = "numa", .cb = handle_yaml_del_numa },
3888 { .name = "ping", .cb = handle_yaml_no_op },
3889 { .name = "discover", .cb = handle_yaml_no_op },
3892 static struct lookup_cmd_hdlr_tbl lookup_show_tbl[] = {
3893 { .name = "route", .cb = handle_yaml_show_route },
3894 { .name = "net", .cb = handle_yaml_show_net },
3895 { .name = "peer", .cb = handle_yaml_show_peers },
3896 { .name = "ip2nets", .cb = handle_yaml_no_op },
3897 { .name = "routing", .cb = handle_yaml_show_routing },
3898 { .name = "buffers", .cb = handle_yaml_show_routing },
3899 { .name = "statistics", .cb = handle_yaml_show_stats },
3900 { .name = "global", .cb = handle_yaml_show_global_settings},
3901 { .name = "numa", .cb = handle_yaml_show_numa },
3902 { .name = "ping", .cb = handle_yaml_no_op },
3903 { .name = "discover", .cb = handle_yaml_no_op },
3906 static struct lookup_cmd_hdlr_tbl lookup_exec_tbl[] = {
3907 { .name = "route", .cb = handle_yaml_no_op },
3908 { .name = "net", .cb = handle_yaml_no_op },
3909 { .name = "peer", .cb = handle_yaml_no_op },
3910 { .name = "ip2nets", .cb = handle_yaml_no_op },
3911 { .name = "routing", .cb = handle_yaml_no_op },
3912 { .name = "buffers", .cb = handle_yaml_no_op },
3913 { .name = "statistics", .cb = handle_yaml_no_op },
3914 { .name = "global", .cb = handle_yaml_no_op },
3915 { .name = "numa", .cb = handle_yaml_no_op },
3916 { .name = "ping", .cb = handle_yaml_ping },
3917 { .name = "discover", .cb = handle_yaml_discover },
3920 static cmd_handler_t lookup_fn(char *key,
3921 struct lookup_cmd_hdlr_tbl *tbl)
3927 for (i = 0; tbl[i].name != NULL; i++) {
3928 if (strncmp(key, tbl[i].name, strlen(tbl[i].name)) == 0)
3935 static int lustre_yaml_cb_helper(char *f, struct lookup_cmd_hdlr_tbl *table,
3936 struct cYAML **show_rc, struct cYAML **err_rc)
3938 struct cYAML *tree, *item = NULL, *head, *child;
3940 char err_str[LNET_MAX_STR_LEN];
3941 int rc = LUSTRE_CFG_RC_NO_ERR, return_rc = LUSTRE_CFG_RC_NO_ERR;
3943 tree = cYAML_build_tree(f, NULL, 0, err_rc, false);
3945 return LUSTRE_CFG_RC_BAD_PARAM;
3947 child = tree->cy_child;
3948 while (child != NULL) {
3949 cb = lookup_fn(child->cy_string, table);
3951 snprintf(err_str, sizeof(err_str),
3952 "\"call back for '%s' not found\"",
3954 cYAML_build_error(LUSTRE_CFG_RC_BAD_PARAM, -1,
3955 "yaml", "helper", err_str, err_rc);
3959 if (cYAML_is_sequence(child)) {
3960 while ((head = cYAML_get_next_seq_item(child, &item))
3962 rc = cb(head, show_rc, err_rc);
3963 if (rc != LUSTRE_CFG_RC_NO_ERR)
3967 rc = cb(child, show_rc, err_rc);
3968 if (rc != LUSTRE_CFG_RC_NO_ERR)
3972 child = child->cy_next;
3976 cYAML_free_tree(tree);
3981 int lustre_yaml_config(char *f, struct cYAML **err_rc)
3983 return lustre_yaml_cb_helper(f, lookup_config_tbl,
3987 int lustre_yaml_del(char *f, struct cYAML **err_rc)
3989 return lustre_yaml_cb_helper(f, lookup_del_tbl,
3993 int lustre_yaml_show(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
3995 return lustre_yaml_cb_helper(f, lookup_show_tbl,
3999 int lustre_yaml_exec(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
4001 return lustre_yaml_cb_helper(f, lookup_exec_tbl,