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"
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 lnet_process_id_t 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(lnet_process_id_t) * 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(s): '%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(s): '%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\"");
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 || nw_descr->nw_id == 0 ||
1684 list_empty(&nw_descr->nw_intflist)) {
1687 "\"missing mandatory parameter\"");
1688 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1692 if (LNET_NETTYP(nw_descr->nw_id) == LOLND)
1693 return LUSTRE_CFG_RC_NO_ERR;
1695 if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1698 "\"cannot parse net '%s'\"",
1699 libcfs_net2str(nw_descr->nw_id));
1700 rc = LUSTRE_CFG_RC_BAD_PARAM;
1704 rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids);
1706 snprintf(err_str, sizeof(err_str),
1707 "\"bad parameter\"");
1708 rc = LUSTRE_CFG_RC_BAD_PARAM;
1713 * no interfaces just the nw_id is specified
1716 nids = calloc(1, sizeof(*nids));
1718 snprintf(err_str, sizeof(err_str),
1719 "\"out of memory\"");
1720 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1723 nids[0] = LNET_MKNID(nw_descr->nw_id, 0);
1727 for (i = 0; i < nnids; i++) {
1728 LIBCFS_IOC_INIT_V2(data, lic_cfg_hdr);
1729 data.lic_nid = nids[i];
1731 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_LOCAL_NI, &data);
1736 "\"cannot del network: %s\"", strerror(errno));
1740 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
1742 list_del(&intf_descr->intf_on_network);
1743 free_intf_descr(intf_descr);
1747 cYAML_build_error(rc, seq_no, DEL_CMD, "net", err_str, err_rc);
1756 add_msg_stats_to_yaml_blk(struct cYAML *yaml,
1757 struct lnet_ioctl_comm_count *counts)
1759 if (cYAML_create_number(yaml, "put",
1760 counts->ico_put_count)
1763 if (cYAML_create_number(yaml, "get",
1764 counts->ico_get_count)
1767 if (cYAML_create_number(yaml, "reply",
1768 counts->ico_reply_count)
1771 if (cYAML_create_number(yaml, "ack",
1772 counts->ico_ack_count)
1775 if (cYAML_create_number(yaml, "hello",
1776 counts->ico_hello_count)
1783 static struct lnet_ioctl_comm_count *
1784 get_counts(struct lnet_ioctl_element_msg_stats *msg_stats, int idx)
1787 return &msg_stats->im_send_stats;
1789 return &msg_stats->im_recv_stats;
1791 return &msg_stats->im_drop_stats;
1796 int lustre_lnet_show_net(char *nw, int detail, int seq_no,
1797 struct cYAML **show_rc, struct cYAML **err_rc)
1800 struct lnet_ioctl_config_ni *ni_data;
1801 struct lnet_ioctl_config_lnd_tunables *lnd;
1802 struct lnet_ioctl_element_stats *stats;
1803 struct lnet_ioctl_element_msg_stats msg_stats;
1804 __u32 net = LNET_NIDNET(LNET_NID_ANY);
1805 __u32 prev_net = LNET_NIDNET(LNET_NID_ANY);
1806 int rc = LUSTRE_CFG_RC_OUT_OF_MEM, i, j;
1808 struct cYAML *root = NULL, *tunables = NULL,
1809 *net_node = NULL, *interfaces = NULL,
1810 *item = NULL, *first_seq = NULL,
1811 *tmp = NULL, *statistics = NULL;
1812 int str_buf_len = LNET_MAX_SHOW_NUM_CPT * 2;
1813 char str_buf[str_buf_len];
1815 char err_str[LNET_MAX_STR_LEN];
1816 bool exist = false, new_net = true;
1818 size_t buf_size = sizeof(*ni_data) + sizeof(*lnd) + sizeof(*stats);
1820 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1822 buf = calloc(1, buf_size);
1826 ni_data = (struct lnet_ioctl_config_ni *)buf;
1829 net = libcfs_str2net(nw);
1830 if (net == LNET_NIDNET(LNET_NID_ANY)) {
1833 "\"cannot parse net '%s'\"", nw);
1834 rc = LUSTRE_CFG_RC_BAD_PARAM;
1839 root = cYAML_create_object(NULL, NULL);
1843 net_node = cYAML_create_seq(root, "net");
1844 if (net_node == NULL)
1851 memset(buf, 0, buf_size);
1853 LIBCFS_IOC_INIT_V2(*ni_data, lic_cfg_hdr);
1855 * set the ioc_len to the proper value since INIT assumes
1858 ni_data->lic_cfg_hdr.ioc_len = buf_size;
1859 ni_data->lic_idx = i;
1861 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LOCAL_NI, ni_data);
1867 rc_net = LNET_NIDNET(ni_data->lic_nid);
1869 /* filter on provided data */
1870 if (net != LNET_NIDNET(LNET_NID_ANY) &&
1874 /* default rc to -1 in case we hit the goto */
1878 stats = (struct lnet_ioctl_element_stats *)ni_data->lic_bulk;
1879 lnd = (struct lnet_ioctl_config_lnd_tunables *)
1880 (ni_data->lic_bulk + sizeof(*stats));
1882 if (rc_net != prev_net) {
1889 if (!cYAML_create_string(net_node, "net type",
1890 libcfs_net2str(rc_net)))
1893 tmp = cYAML_create_seq(net_node, "local NI(s)");
1899 /* create the tree to be printed. */
1900 item = cYAML_create_seq_item(tmp);
1904 if (first_seq == NULL)
1907 if (cYAML_create_string(item, "nid",
1908 libcfs_nid2str(ni_data->lic_nid)) == NULL)
1911 if (cYAML_create_string(item,
1913 (ni_data->lic_status ==
1914 LNET_NI_STATUS_UP) ?
1915 "up" : "down") == NULL)
1918 /* don't add interfaces unless there is at least one
1920 if (strlen(ni_data->lic_ni_intf[0]) > 0) {
1921 interfaces = cYAML_create_object(item, "interfaces");
1922 if (interfaces == NULL)
1925 for (j = 0; j < LNET_INTERFACES_NUM; j++) {
1926 if (strlen(ni_data->lic_ni_intf[j]) > 0) {
1928 sizeof(str_buf), "%d", j);
1929 if (cYAML_create_string(interfaces,
1931 ni_data->lic_ni_intf[j]) ==
1942 statistics = cYAML_create_object(item, "statistics");
1943 if (statistics == NULL)
1946 if (cYAML_create_number(statistics, "send_count",
1947 stats->iel_send_count)
1951 if (cYAML_create_number(statistics, "recv_count",
1952 stats->iel_recv_count)
1956 if (cYAML_create_number(statistics, "drop_count",
1957 stats->iel_drop_count)
1962 goto continue_without_msg_stats;
1964 LIBCFS_IOC_INIT_V2(msg_stats, im_hdr);
1965 msg_stats.im_hdr.ioc_len = sizeof(msg_stats);
1966 msg_stats.im_idx = i;
1968 rc = l_ioctl(LNET_DEV_ID,
1969 IOC_LIBCFS_GET_LOCAL_NI_MSG_STATS,
1973 goto continue_without_msg_stats;
1976 for (k = 0; k < 3; k++) {
1977 struct lnet_ioctl_comm_count *counts;
1978 struct cYAML *msg_statistics = NULL;
1980 msg_statistics = cYAML_create_object(item,
1981 (char *)gmsg_stat_names[k]);
1982 if (msg_statistics == NULL)
1985 counts = get_counts(&msg_stats, k);
1989 if (!add_msg_stats_to_yaml_blk(msg_statistics,
1994 continue_without_msg_stats:
1995 tunables = cYAML_create_object(item, "tunables");
1999 rc = lustre_net_show_tunables(tunables, &lnd->lt_cmn);
2000 if (rc != LUSTRE_CFG_RC_NO_ERR)
2003 tunables = cYAML_create_object(item, "lnd tunables");
2004 if (tunables == NULL)
2007 rc = lustre_ni_show_tunables(tunables, LNET_NETTYP(rc_net),
2009 if (rc != LUSTRE_CFG_RC_NO_ERR)
2012 if (cYAML_create_number(item, "tcp bonding",
2013 ni_data->lic_tcp_bonding)
2017 if (cYAML_create_number(item, "dev cpt",
2018 ni_data->lic_dev_cpt) == NULL)
2021 /* out put the CPTs in the format: "[x,x,x,...]" */
2022 limit = str_buf + str_buf_len - 3;
2023 pos += snprintf(pos, limit - pos, "\"[");
2024 for (j = 0 ; ni_data->lic_ncpts >= 1 &&
2025 j < ni_data->lic_ncpts &&
2027 pos += snprintf(pos, limit - pos,
2028 "%d", ni_data->lic_cpts[j]);
2029 if ((j + 1) < ni_data->lic_ncpts)
2030 pos += snprintf(pos, limit - pos, ",");
2032 pos += snprintf(pos, 3, "]\"");
2034 if (ni_data->lic_ncpts >= 1 &&
2035 cYAML_create_string(item, "CPT",
2041 /* Print out the net information only if show_rc is not provided */
2042 if (show_rc == NULL)
2043 cYAML_print_tree(root);
2045 if (l_errno != ENOENT) {
2048 "\"cannot get networks: %s\"",
2053 rc = LUSTRE_CFG_RC_NO_ERR;
2055 snprintf(err_str, sizeof(err_str), "\"success\"");
2057 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2058 cYAML_free_tree(root);
2059 } else if (show_rc != NULL && *show_rc != NULL) {
2060 struct cYAML *show_node;
2061 /* find the net node, if one doesn't exist
2062 * then insert one. Otherwise add to the one there
2064 show_node = cYAML_get_object_item(*show_rc, "net");
2065 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2066 cYAML_insert_child(show_node, first_seq);
2069 } else if (show_node == NULL) {
2070 cYAML_insert_sibling((*show_rc)->cy_child,
2074 cYAML_free_tree(root);
2080 cYAML_build_error(rc, seq_no, SHOW_CMD, "net", err_str, err_rc);
2085 int lustre_lnet_enable_routing(int enable, int seq_no, struct cYAML **err_rc)
2087 struct lnet_ioctl_config_data data;
2088 int rc = LUSTRE_CFG_RC_NO_ERR;
2089 char err_str[LNET_MAX_STR_LEN];
2091 snprintf(err_str, sizeof(err_str), "\"success\"");
2093 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2094 data.cfg_config_u.cfg_buffers.buf_enable = (enable) ? 1 : 0;
2096 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIG_RTR, &data);
2101 "\"cannot %s routing %s\"",
2102 (enable) ? "enable" : "disable", strerror(errno));
2107 cYAML_build_error(rc, seq_no,
2108 (enable) ? ADD_CMD : DEL_CMD,
2109 "routing", err_str, err_rc);
2114 int ioctl_set_value(__u32 val, int ioc, char *name,
2115 int seq_no, struct cYAML **err_rc)
2117 struct lnet_ioctl_set_value data;
2118 int rc = LUSTRE_CFG_RC_NO_ERR;
2119 char err_str[LNET_MAX_STR_LEN];
2121 snprintf(err_str, sizeof(err_str), "\"success\"");
2123 LIBCFS_IOC_INIT_V2(data, sv_hdr);
2124 data.sv_value = val;
2126 rc = l_ioctl(LNET_DEV_ID, ioc , &data);
2131 "\"cannot configure %s to %d: %s\"", name,
2132 val, strerror(errno));
2135 cYAML_build_error(rc, seq_no, ADD_CMD, name, err_str, err_rc);
2140 int lustre_lnet_config_max_intf(int max, int seq_no, struct cYAML **err_rc)
2142 int rc = LUSTRE_CFG_RC_NO_ERR;
2143 char err_str[LNET_MAX_STR_LEN];
2144 char val[LNET_MAX_STR_LEN];
2146 snprintf(err_str, sizeof(err_str), "\"success\"");
2148 snprintf(val, sizeof(val), "%d", max);
2150 rc = write_sysfs_file(modparam_path, "lnet_interfaces_max", val,
2151 1, strlen(val) + 1);
2153 snprintf(err_str, sizeof(err_str),
2154 "\"cannot configure max interfaces: %s\"",
2157 cYAML_build_error(rc, seq_no, ADD_CMD, "max_interfaces", err_str, err_rc);
2162 int lustre_lnet_config_discovery(int enable, int seq_no, struct cYAML **err_rc)
2164 int rc = LUSTRE_CFG_RC_NO_ERR;
2165 char err_str[LNET_MAX_STR_LEN];
2166 char val[LNET_MAX_STR_LEN];
2168 snprintf(err_str, sizeof(err_str), "\"success\"");
2170 snprintf(val, sizeof(val), "%u", (enable) ? 0 : 1);
2172 rc = write_sysfs_file(modparam_path, "lnet_peer_discovery_disabled", val,
2173 1, strlen(val) + 1);
2175 snprintf(err_str, sizeof(err_str),
2176 "\"cannot configure discovery: %s\"",
2179 cYAML_build_error(rc, seq_no, ADD_CMD, "discovery", err_str, err_rc);
2185 int lustre_lnet_config_numa_range(int range, int seq_no, struct cYAML **err_rc)
2187 return ioctl_set_value(range, IOC_LIBCFS_SET_NUMA_RANGE,
2188 "numa_range", seq_no, err_rc);
2191 int lustre_lnet_config_buffers(int tiny, int small, int large, int seq_no,
2192 struct cYAML **err_rc)
2194 struct lnet_ioctl_config_data data;
2195 int rc = LUSTRE_CFG_RC_NO_ERR;
2196 char err_str[LNET_MAX_STR_LEN];
2198 snprintf(err_str, sizeof(err_str), "\"success\"");
2200 /* -1 indicates to ignore changes to this field */
2201 if (tiny < -1 || small < -1 || large < -1) {
2204 "\"tiny, small and large must be >= 0\"");
2205 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
2209 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2210 data.cfg_config_u.cfg_buffers.buf_tiny = tiny;
2211 data.cfg_config_u.cfg_buffers.buf_small = small;
2212 data.cfg_config_u.cfg_buffers.buf_large = large;
2214 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_BUF, &data);
2219 "\"cannot configure buffers: %s\"", strerror(errno));
2224 cYAML_build_error(rc, seq_no, ADD_CMD, "buf", err_str, err_rc);
2229 int lustre_lnet_show_routing(int seq_no, struct cYAML **show_rc,
2230 struct cYAML **err_rc)
2232 struct lnet_ioctl_config_data *data;
2233 struct lnet_ioctl_pool_cfg *pool_cfg = NULL;
2234 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2237 char *pools[LNET_NRBPOOLS] = {"tiny", "small", "large"};
2238 int buf_count[LNET_NRBPOOLS] = {0};
2239 struct cYAML *root = NULL, *pools_node = NULL,
2240 *type_node = NULL, *item = NULL, *cpt = NULL,
2241 *first_seq = NULL, *buffers = NULL;
2243 char err_str[LNET_MAX_STR_LEN];
2244 char node_name[LNET_MAX_STR_LEN];
2247 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2249 buf = calloc(1, sizeof(*data) + sizeof(*pool_cfg));
2253 data = (struct lnet_ioctl_config_data *)buf;
2255 root = cYAML_create_object(NULL, NULL);
2259 pools_node = cYAML_create_seq(root, "routing");
2260 if (pools_node == NULL)
2264 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
2265 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
2266 sizeof(struct lnet_ioctl_pool_cfg);
2267 data->cfg_count = i;
2269 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_BUF, data);
2277 pool_cfg = (struct lnet_ioctl_pool_cfg *)data->cfg_bulk;
2279 snprintf(node_name, sizeof(node_name), "cpt[%d]", i);
2280 item = cYAML_create_seq_item(pools_node);
2284 if (first_seq == NULL)
2287 cpt = cYAML_create_object(item, node_name);
2291 /* create the tree and print */
2292 for (j = 0; j < LNET_NRBPOOLS; j++) {
2293 type_node = cYAML_create_object(cpt, pools[j]);
2294 if (type_node == NULL)
2296 if (cYAML_create_number(type_node, "npages",
2297 pool_cfg->pl_pools[j].pl_npages)
2300 if (cYAML_create_number(type_node, "nbuffers",
2301 pool_cfg->pl_pools[j].
2302 pl_nbuffers) == NULL)
2304 if (cYAML_create_number(type_node, "credits",
2305 pool_cfg->pl_pools[j].
2306 pl_credits) == NULL)
2308 if (cYAML_create_number(type_node, "mincredits",
2309 pool_cfg->pl_pools[j].
2310 pl_mincredits) == NULL)
2312 /* keep track of the total count for each of the
2313 * tiny, small and large buffers */
2314 buf_count[j] += pool_cfg->pl_pools[j].pl_nbuffers;
2318 if (pool_cfg != NULL) {
2319 item = cYAML_create_seq_item(pools_node);
2323 if (cYAML_create_number(item, "enable", pool_cfg->pl_routing) ==
2328 /* create a buffers entry in the show. This is necessary so that
2329 * if the YAML output is used to configure a node, the buffer
2330 * configuration takes hold */
2331 buffers = cYAML_create_object(root, "buffers");
2332 if (buffers == NULL)
2335 for (i = 0; i < LNET_NRBPOOLS; i++) {
2336 if (cYAML_create_number(buffers, pools[i], buf_count[i]) == NULL)
2340 if (show_rc == NULL)
2341 cYAML_print_tree(root);
2343 if (l_errno != ENOENT) {
2346 "\"cannot get routing information: %s\"",
2351 rc = LUSTRE_CFG_RC_NO_ERR;
2353 snprintf(err_str, sizeof(err_str), "\"success\"");
2354 rc = LUSTRE_CFG_RC_NO_ERR;
2358 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2359 cYAML_free_tree(root);
2360 } else if (show_rc != NULL && *show_rc != NULL) {
2361 struct cYAML *routing_node;
2362 /* there should exist only one routing block and one
2363 * buffers block. If there already exists a previous one
2364 * then don't add another */
2365 routing_node = cYAML_get_object_item(*show_rc, "routing");
2366 if (routing_node == NULL) {
2367 cYAML_insert_sibling((*show_rc)->cy_child,
2371 cYAML_free_tree(root);
2377 cYAML_build_error(rc, seq_no, SHOW_CMD, "routing", err_str, err_rc);
2382 int lustre_lnet_show_peer(char *knid, int detail, int seq_no,
2383 struct cYAML **show_rc, struct cYAML **err_rc)
2386 * TODO: This function is changing in a future patch to accommodate
2387 * PEER_LIST and proper filtering on any nid of the peer
2389 struct lnet_ioctl_peer_cfg peer_info;
2390 struct lnet_peer_ni_credit_info *lpni_cri;
2391 struct lnet_ioctl_element_stats *lpni_stats;
2392 struct lnet_ioctl_element_msg_stats *msg_stats;
2394 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2399 struct cYAML *root = NULL, *peer = NULL, *peer_ni = NULL,
2400 *first_seq = NULL, *peer_root = NULL, *tmp = NULL,
2401 *msg_statistics = NULL, *statistics = NULL;
2402 char err_str[LNET_MAX_STR_LEN];
2403 lnet_process_id_t *list = NULL;
2407 snprintf(err_str, sizeof(err_str),
2408 "\"out of memory\"");
2410 /* create struct cYAML root object */
2411 root = cYAML_create_object(NULL, NULL);
2415 peer_root = cYAML_create_seq(root, "peer");
2416 if (peer_root == NULL)
2420 size = count * sizeof(lnet_process_id_t);
2421 list = malloc(size);
2427 list[0].nid = libcfs_str2nid(knid);
2431 memset(&peer_info, 0, sizeof(peer_info));
2432 LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2433 peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2434 peer_info.prcfg_size = size;
2435 peer_info.prcfg_bulk = list;
2438 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_LIST,
2440 count = peer_info.prcfg_count;
2444 if (l_errno != E2BIG) {
2447 "\"cannot get peer list: %s\"",
2453 size = peer_info.prcfg_size;
2454 list = malloc(size);
2463 data = malloc(size);
2468 for (i = 0; i < count; i++) {
2470 memset(&peer_info, 0, sizeof(peer_info));
2471 LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2472 peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2473 peer_info.prcfg_prim_nid = list[i].nid;
2474 peer_info.prcfg_size = size;
2475 peer_info.prcfg_bulk = data;
2478 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_NI,
2483 if (l_errno != E2BIG) {
2486 "\"cannot get peer information: %s\"",
2492 size = peer_info.prcfg_size;
2493 data = malloc(size);
2500 peer = cYAML_create_seq_item(peer_root);
2504 if (first_seq == NULL)
2507 lnet_nid_t pnid = peer_info.prcfg_prim_nid;
2508 if (cYAML_create_string(peer, "primary nid",
2509 libcfs_nid2str(pnid))
2512 if (cYAML_create_string(peer, "Multi-Rail",
2513 peer_info.prcfg_mr ? "True" : "False")
2517 * print out the state of the peer only if details are
2521 if (cYAML_create_number(peer, "peer state",
2522 peer_info.prcfg_state)
2527 tmp = cYAML_create_seq(peer, "peer ni");
2532 for (j = 0; j < peer_info.prcfg_count; j++) {
2534 lpni_cri = (void*)nidp + sizeof(nidp);
2535 lpni_stats = (void *)lpni_cri + sizeof(*lpni_cri);
2536 msg_stats = (void *)lpni_stats + sizeof(*lpni_stats);
2537 lpni_data = (void *)msg_stats + sizeof(*msg_stats);
2539 peer_ni = cYAML_create_seq_item(tmp);
2540 if (peer_ni == NULL)
2543 if (cYAML_create_string(peer_ni, "nid",
2544 libcfs_nid2str(*nidp))
2548 if (cYAML_create_string(peer_ni, "state",
2549 lpni_cri->cr_aliveness)
2556 if (cYAML_create_number(peer_ni, "max_ni_tx_credits",
2557 lpni_cri->cr_ni_peer_tx_credits)
2561 if (cYAML_create_number(peer_ni, "available_tx_credits",
2562 lpni_cri->cr_peer_tx_credits)
2566 if (cYAML_create_number(peer_ni, "min_tx_credits",
2567 lpni_cri->cr_peer_min_tx_credits)
2571 if (cYAML_create_number(peer_ni, "tx_q_num_of_buf",
2572 lpni_cri->cr_peer_tx_qnob)
2576 if (cYAML_create_number(peer_ni, "available_rtr_credits",
2577 lpni_cri->cr_peer_rtr_credits)
2581 if (cYAML_create_number(peer_ni, "min_rtr_credits",
2582 lpni_cri->cr_peer_min_rtr_credits)
2586 if (cYAML_create_number(peer_ni, "refcount",
2587 lpni_cri->cr_refcount) == NULL)
2590 statistics = cYAML_create_object(peer_ni, "statistics");
2591 if (statistics == NULL)
2594 if (cYAML_create_number(statistics, "send_count",
2595 lpni_stats->iel_send_count)
2599 if (cYAML_create_number(statistics, "recv_count",
2600 lpni_stats->iel_recv_count)
2604 if (cYAML_create_number(statistics, "drop_count",
2605 lpni_stats->iel_drop_count)
2612 for (k = 0; k < 3; k++) {
2613 struct lnet_ioctl_comm_count *counts;
2615 msg_statistics = cYAML_create_object(peer_ni,
2616 (char *) gmsg_stat_names[k]);
2617 if (msg_statistics == NULL)
2620 counts = get_counts(msg_stats, k);
2624 if (!add_msg_stats_to_yaml_blk(msg_statistics,
2632 /* print output iff show_rc is not provided */
2633 if (show_rc == NULL)
2634 cYAML_print_tree(root);
2636 snprintf(err_str, sizeof(err_str), "\"success\"");
2637 rc = LUSTRE_CFG_RC_NO_ERR;
2642 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2643 cYAML_free_tree(root);
2644 } else if (show_rc != NULL && *show_rc != NULL) {
2645 struct cYAML *show_node;
2646 /* find the peer node, if one doesn't exist then
2647 * insert one. Otherwise add to the one there
2649 show_node = cYAML_get_object_item(*show_rc,
2651 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2652 cYAML_insert_child(show_node, first_seq);
2655 } else if (show_node == NULL) {
2656 cYAML_insert_sibling((*show_rc)->cy_child,
2660 cYAML_free_tree(root);
2666 cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
2672 int lustre_lnet_list_peer(int seq_no,
2673 struct cYAML **show_rc, struct cYAML **err_rc)
2675 struct lnet_ioctl_peer_cfg peer_info;
2676 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2681 struct cYAML *root = NULL, *list_root = NULL, *first_seq = NULL;
2682 char err_str[LNET_MAX_STR_LEN];
2683 lnet_process_id_t *list = NULL;
2685 snprintf(err_str, sizeof(err_str),
2686 "\"out of memory\"");
2688 memset(&peer_info, 0, sizeof(peer_info));
2690 /* create struct cYAML root object */
2691 root = cYAML_create_object(NULL, NULL);
2695 list_root = cYAML_create_seq(root, "peer list");
2696 if (list_root == NULL)
2700 size = count * sizeof(lnet_process_id_t);
2701 list = malloc(size);
2707 LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2708 peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2709 peer_info.prcfg_size = size;
2710 peer_info.prcfg_bulk = list;
2713 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_LIST, &peer_info);
2714 count = peer_info.prcfg_count;
2718 if (l_errno != E2BIG) {
2721 "\"cannot get peer list: %s\"",
2727 size = peer_info.prcfg_size;
2728 list = malloc(size);
2735 /* count is now the actual number of ids in the list. */
2736 for (i = 0; i < count; i++) {
2737 if (cYAML_create_string(list_root, "nid",
2738 libcfs_nid2str(list[i].nid))
2743 /* print output iff show_rc is not provided */
2744 if (show_rc == NULL)
2745 cYAML_print_tree(root);
2747 snprintf(err_str, sizeof(err_str), "\"success\"");
2748 rc = LUSTRE_CFG_RC_NO_ERR;
2753 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2754 cYAML_free_tree(root);
2755 } else if (show_rc != NULL && *show_rc != NULL) {
2756 struct cYAML *show_node;
2757 /* find the peer node, if one doesn't exist then
2758 * insert one. Otherwise add to the one there
2760 show_node = cYAML_get_object_item(*show_rc,
2762 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2763 cYAML_insert_child(show_node, first_seq);
2766 } else if (show_node == NULL) {
2767 cYAML_insert_sibling((*show_rc)->cy_child,
2771 cYAML_free_tree(root);
2777 cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
2783 static void add_to_global(struct cYAML *show_rc, struct cYAML *node,
2786 struct cYAML *show_node;
2788 show_node = cYAML_get_object_item(show_rc, "global");
2789 if (show_node != NULL)
2790 cYAML_insert_sibling(show_node->cy_child,
2793 cYAML_insert_sibling(show_rc->cy_child,
2798 static int build_global_yaml_entry(char *err_str, int err_len, int seq_no,
2799 char *name, __u32 value,
2800 struct cYAML **show_rc,
2801 struct cYAML **err_rc, int err)
2803 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2804 struct cYAML *root = NULL, *global = NULL;
2811 root = cYAML_create_object(NULL, NULL);
2815 global = cYAML_create_object(root, "global");
2819 if (cYAML_create_number(global, name,
2823 if (show_rc == NULL)
2824 cYAML_print_tree(root);
2826 snprintf(err_str, err_len, "\"success\"");
2828 rc = LUSTRE_CFG_RC_NO_ERR;
2831 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2832 cYAML_free_tree(root);
2833 } else if (show_rc != NULL && *show_rc != NULL) {
2834 add_to_global(*show_rc, global, root);
2839 cYAML_build_error(rc, seq_no, SHOW_CMD, "global", err_str, err_rc);
2844 static int ioctl_show_global_values(int ioc, int seq_no, char *name,
2845 struct cYAML **show_rc,
2846 struct cYAML **err_rc)
2848 struct lnet_ioctl_set_value data;
2851 char err_str[LNET_MAX_STR_LEN];
2853 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2855 LIBCFS_IOC_INIT_V2(data, sv_hdr);
2857 rc = l_ioctl(LNET_DEV_ID, ioc, &data);
2862 "\"cannot get %s: %s\"",
2863 name, strerror(l_errno));
2866 return build_global_yaml_entry(err_str, sizeof(err_str), seq_no, name,
2867 data.sv_value, show_rc, err_rc, l_errno);
2870 int lustre_lnet_show_max_intf(int seq_no, struct cYAML **show_rc,
2871 struct cYAML **err_rc)
2873 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2874 char val[LNET_MAX_STR_LEN];
2875 int max_intf = -1, l_errno = 0;
2876 char err_str[LNET_MAX_STR_LEN];
2878 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2880 rc = read_sysfs_file(modparam_path, "lnet_interfaces_max", val,
2884 snprintf(err_str, sizeof(err_str),
2885 "\"cannot get max interfaces: %d\"", rc);
2887 max_intf = atoi(val);
2890 return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
2891 "max_intf", max_intf, show_rc,
2895 int lustre_lnet_show_discovery(int seq_no, struct cYAML **show_rc,
2896 struct cYAML **err_rc)
2898 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2899 char val[LNET_MAX_STR_LEN];
2900 int discovery = -1, l_errno = 0;
2901 char err_str[LNET_MAX_STR_LEN];
2903 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2905 rc = read_sysfs_file(modparam_path, "lnet_peer_discovery_disabled", val,
2909 snprintf(err_str, sizeof(err_str),
2910 "\"cannot get discovery setting: %d\"", rc);
2913 * The kernel stores a discovery disabled value. User space
2914 * shows whether discovery is enabled. So the value must be
2917 discovery = !atoi(val);
2920 return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
2921 "discovery", discovery, show_rc,
2925 int lustre_lnet_show_numa_range(int seq_no, struct cYAML **show_rc,
2926 struct cYAML **err_rc)
2928 return ioctl_show_global_values(IOC_LIBCFS_GET_NUMA_RANGE, seq_no,
2929 "numa_range", show_rc, err_rc);
2932 int lustre_lnet_show_stats(int seq_no, struct cYAML **show_rc,
2933 struct cYAML **err_rc)
2935 struct lnet_ioctl_lnet_stats data;
2938 char err_str[LNET_MAX_STR_LEN];
2939 struct cYAML *root = NULL, *stats = NULL;
2941 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2943 LIBCFS_IOC_INIT_V2(data, st_hdr);
2945 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LNET_STATS, &data);
2950 "\"cannot get lnet statistics: %s\"",
2956 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2958 root = cYAML_create_object(NULL, NULL);
2962 stats = cYAML_create_object(root, "statistics");
2966 if (cYAML_create_number(stats, "msgs_alloc",
2967 data.st_cntrs.msgs_alloc) == NULL)
2970 if (cYAML_create_number(stats, "msgs_max",
2971 data.st_cntrs.msgs_max) == NULL)
2974 if (cYAML_create_number(stats, "errors",
2975 data.st_cntrs.errors) == NULL)
2978 if (cYAML_create_number(stats, "send_count",
2979 data.st_cntrs.send_count) == NULL)
2982 if (cYAML_create_number(stats, "recv_count",
2983 data.st_cntrs.recv_count) == NULL)
2986 if (cYAML_create_number(stats, "route_count",
2987 data.st_cntrs.route_count) == NULL)
2990 if (cYAML_create_number(stats, "drop_count",
2991 data.st_cntrs.drop_count) == NULL)
2994 if (cYAML_create_number(stats, "send_length",
2995 data.st_cntrs.send_length) == NULL)
2998 if (cYAML_create_number(stats, "recv_length",
2999 data.st_cntrs.recv_length) == NULL)
3002 if (cYAML_create_number(stats, "route_length",
3003 data.st_cntrs.route_length) == NULL)
3006 if (cYAML_create_number(stats, "drop_length",
3007 data.st_cntrs.drop_length) == NULL)
3010 if (show_rc == NULL)
3011 cYAML_print_tree(root);
3013 snprintf(err_str, sizeof(err_str), "\"success\"");
3014 rc = LUSTRE_CFG_RC_NO_ERR;
3016 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
3017 cYAML_free_tree(root);
3018 } else if (show_rc != NULL && *show_rc != NULL) {
3019 cYAML_insert_sibling((*show_rc)->cy_child,
3026 cYAML_build_error(rc, seq_no, SHOW_CMD, "statistics", err_str, err_rc);
3031 typedef int (*cmd_handler_t)(struct cYAML *tree,
3032 struct cYAML **show_rc,
3033 struct cYAML **err_rc);
3035 static int handle_yaml_config_route(struct cYAML *tree, struct cYAML **show_rc,
3036 struct cYAML **err_rc)
3038 struct cYAML *net, *gw, *hop, *prio, *seq_no;
3040 net = cYAML_get_object_item(tree, "net");
3041 gw = cYAML_get_object_item(tree, "gateway");
3042 hop = cYAML_get_object_item(tree, "hop");
3043 prio = cYAML_get_object_item(tree, "priority");
3044 seq_no = cYAML_get_object_item(tree, "seq_no");
3046 return lustre_lnet_config_route((net) ? net->cy_valuestring : NULL,
3047 (gw) ? gw->cy_valuestring : NULL,
3048 (hop) ? hop->cy_valueint : -1,
3049 (prio) ? prio->cy_valueint : -1,
3050 (seq_no) ? seq_no->cy_valueint : -1,
3054 static void yaml_free_string_array(char **array, int num)
3057 char **sub_array = array;
3059 for (i = 0; i < num; i++) {
3060 if (*sub_array != NULL)
3070 * 0: <intf_name>['['<expr>']']
3071 * 1: <intf_name>['['<expr>']']
3073 static int yaml_copy_intf_info(struct cYAML *intf_tree,
3074 struct lnet_dlc_network_descr *nw_descr)
3076 struct cYAML *child = NULL;
3077 int intf_num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
3078 struct lnet_dlc_intf_descr *intf_descr, *tmp;
3080 if (intf_tree == NULL || nw_descr == NULL)
3081 return LUSTRE_CFG_RC_BAD_PARAM;
3083 /* now grab all the interfaces and their cpts */
3084 child = intf_tree->cy_child;
3085 while (child != NULL) {
3086 if (child->cy_valuestring == NULL) {
3087 child = child->cy_next;
3091 if (strlen(child->cy_valuestring) >= LNET_MAX_STR_LEN)
3094 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist,
3095 child->cy_valuestring,
3096 strlen(child->cy_valuestring));
3097 if (rc != LUSTRE_CFG_RC_NO_ERR)
3101 child = child->cy_next;
3105 return LUSTRE_CFG_RC_MISSING_PARAM;
3110 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
3112 list_del(&intf_descr->intf_on_network);
3113 free_intf_descr(intf_descr);
3120 yaml_extract_cmn_tunables(struct cYAML *tree,
3121 struct lnet_ioctl_config_lnd_cmn_tunables *tunables,
3122 struct cfs_expr_list **global_cpts)
3124 struct cYAML *tun, *item, *smp;
3127 tun = cYAML_get_object_item(tree, "tunables");
3129 item = cYAML_get_object_item(tun, "peer_timeout");
3131 tunables->lct_peer_timeout = item->cy_valueint;
3132 item = cYAML_get_object_item(tun, "peer_credits");
3134 tunables->lct_peer_tx_credits = item->cy_valueint;
3135 item = cYAML_get_object_item(tun, "peer_buffer_credits");
3137 tunables->lct_peer_rtr_credits = item->cy_valueint;
3138 item = cYAML_get_object_item(tun, "credits");
3140 tunables->lct_max_tx_credits = item->cy_valueint;
3141 smp = cYAML_get_object_item(tun, "CPT");
3143 rc = cfs_expr_list_parse(smp->cy_valuestring,
3144 strlen(smp->cy_valuestring),
3145 0, UINT_MAX, global_cpts);
3147 *global_cpts = NULL;
3157 yaml_extract_tunables(struct cYAML *tree,
3158 struct lnet_ioctl_config_lnd_tunables *tunables,
3159 struct cfs_expr_list **global_cpts,
3164 rc = yaml_extract_cmn_tunables(tree, &tunables->lt_cmn,
3170 lustre_yaml_extract_lnd_tunables(tree, net_type,
3178 * - net type: <net>[<NUM>]
3180 * - nid: <ip>@<net>[<NUM>]
3183 * 0: <intf_name>['['<expr>']']
3184 * 1: <intf_name>['['<expr>']']
3186 * peer_timeout: <NUM>
3187 * peer_credits: <NUM>
3188 * peer_buffer_credits: <NUM>
3191 * peercredits_hiw: <NUM>
3192 * map_on_demand: <NUM>
3193 * concurrent_sends: <NUM>
3194 * fmr_pool_size: <NUM>
3195 * fmr_flush_trigger: <NUM>
3198 * At least one interface is required. If no interfaces are provided the
3199 * network interface can not be configured.
3201 static int handle_yaml_config_ni(struct cYAML *tree, struct cYAML **show_rc,
3202 struct cYAML **err_rc)
3204 struct cYAML *net, *intf, *seq_no, *ip2net = NULL, *local_nis = NULL,
3206 int num_entries = 0, rc;
3207 struct lnet_dlc_network_descr nw_descr;
3208 struct cfs_expr_list *global_cpts = NULL;
3209 struct lnet_ioctl_config_lnd_tunables tunables;
3212 memset(&tunables, 0, sizeof(tunables));
3214 INIT_LIST_HEAD(&nw_descr.network_on_rule);
3215 INIT_LIST_HEAD(&nw_descr.nw_intflist);
3217 ip2net = cYAML_get_object_item(tree, "ip2net");
3218 net = cYAML_get_object_item(tree, "net type");
3220 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
3222 nw_descr.nw_id = LOLND;
3225 * if neither net nor ip2nets are present, then we can not
3226 * configure the network.
3228 if (!net && !ip2net)
3229 return LUSTRE_CFG_RC_MISSING_PARAM;
3231 local_nis = cYAML_get_object_item(tree, "local NI(s)");
3232 if (local_nis == NULL)
3233 return LUSTRE_CFG_RC_MISSING_PARAM;
3235 if (!cYAML_is_sequence(local_nis))
3236 return LUSTRE_CFG_RC_BAD_PARAM;
3238 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
3239 intf = cYAML_get_object_item(item, "interfaces");
3242 num_entries = yaml_copy_intf_info(intf, &nw_descr);
3243 if (num_entries <= 0) {
3244 cYAML_build_error(num_entries, -1, "ni", "add",
3245 "bad interface list",
3247 return LUSTRE_CFG_RC_BAD_PARAM;
3251 found = yaml_extract_tunables(tree, &tunables, &global_cpts,
3252 LNET_NETTYP(nw_descr.nw_id));
3253 seq_no = cYAML_get_object_item(tree, "seq_no");
3255 rc = lustre_lnet_config_ni(&nw_descr,
3257 (ip2net) ? ip2net->cy_valuestring : NULL,
3258 (found) ? &tunables: NULL,
3259 (seq_no) ? seq_no->cy_valueint : -1,
3262 if (global_cpts != NULL)
3263 cfs_expr_list_free(global_cpts);
3270 * - net-spec: <tcp|o2ib|gni>[NUM]
3272 * 0: <intf name>['['<expr>']']
3273 * 1: <intf name>['['<expr>']']
3275 * 0: <expr.expr.expr.expr>
3276 * 1: <expr.expr.expr.expr>
3278 static int handle_yaml_config_ip2nets(struct cYAML *tree,
3279 struct cYAML **show_rc,
3280 struct cYAML **err_rc)
3282 struct cYAML *net, *ip_range, *item = NULL, *intf = NULL,
3284 struct lustre_lnet_ip2nets ip2nets;
3285 struct lustre_lnet_ip_range_descr *ip_range_descr = NULL,
3287 int rc = LUSTRE_CFG_RC_NO_ERR;
3288 struct cfs_expr_list *global_cpts = NULL;
3289 struct cfs_expr_list *el, *el_tmp;
3290 struct lnet_ioctl_config_lnd_tunables tunables;
3291 struct lnet_dlc_intf_descr *intf_descr, *intf_tmp;
3294 memset(&tunables, 0, sizeof(tunables));
3296 /* initialize all lists */
3297 INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
3298 INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
3299 INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
3301 net = cYAML_get_object_item(tree, "net-spec");
3303 return LUSTRE_CFG_RC_BAD_PARAM;
3305 if (net != NULL && net->cy_valuestring == NULL)
3306 return LUSTRE_CFG_RC_BAD_PARAM;
3308 /* assign the network id */
3309 ip2nets.ip2nets_net.nw_id = libcfs_str2net(net->cy_valuestring);
3310 if (ip2nets.ip2nets_net.nw_id == LNET_NID_ANY)
3311 return LUSTRE_CFG_RC_BAD_PARAM;
3313 seq_no = cYAML_get_object_item(tree, "seq_no");
3315 intf = cYAML_get_object_item(tree, "interfaces");
3317 rc = yaml_copy_intf_info(intf, &ip2nets.ip2nets_net);
3319 return LUSTRE_CFG_RC_BAD_PARAM;
3322 ip_range = cYAML_get_object_item(tree, "ip-range");
3323 if (ip_range != NULL) {
3324 item = ip_range->cy_child;
3325 while (item != NULL) {
3326 if (item->cy_valuestring == NULL) {
3327 item = item->cy_next;
3331 rc = lustre_lnet_add_ip_range(&ip2nets.ip2nets_ip_ranges,
3332 item->cy_valuestring);
3334 if (rc != LUSTRE_CFG_RC_NO_ERR)
3337 item = item->cy_next;
3341 found = yaml_extract_tunables(tree, &tunables, &global_cpts,
3342 LNET_NETTYP(ip2nets.ip2nets_net.nw_id));
3344 rc = lustre_lnet_config_ip2nets(&ip2nets,
3345 (found) ? &tunables : NULL,
3347 (seq_no) ? seq_no->cy_valueint : -1,
3351 * don't stop because there was no match. Continue processing the
3352 * rest of the rules. If non-match then nothing is configured
3354 if (rc == LUSTRE_CFG_RC_NO_MATCH)
3355 rc = LUSTRE_CFG_RC_NO_ERR;
3357 list_for_each_entry_safe(intf_descr, intf_tmp,
3358 &ip2nets.ip2nets_net.nw_intflist,
3360 list_del(&intf_descr->intf_on_network);
3361 free_intf_descr(intf_descr);
3364 list_for_each_entry_safe(ip_range_descr, tmp,
3365 &ip2nets.ip2nets_ip_ranges,
3367 list_del(&ip_range_descr->ipr_entry);
3368 list_for_each_entry_safe(el, el_tmp, &ip_range_descr->ipr_expr,
3370 list_del(&el->el_link);
3371 cfs_expr_list_free(el);
3373 free(ip_range_descr);
3379 static int handle_yaml_del_ni(struct cYAML *tree, struct cYAML **show_rc,
3380 struct cYAML **err_rc)
3382 struct cYAML *net = NULL, *intf = NULL, *seq_no = NULL, *item = NULL,
3384 int num_entries, rc;
3385 struct lnet_dlc_network_descr nw_descr;
3387 INIT_LIST_HEAD(&nw_descr.network_on_rule);
3388 INIT_LIST_HEAD(&nw_descr.nw_intflist);
3390 net = cYAML_get_object_item(tree, "net type");
3392 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
3394 local_nis = cYAML_get_object_item(tree, "local NI(s)");
3395 if (local_nis == NULL)
3396 return LUSTRE_CFG_RC_MISSING_PARAM;
3398 if (!cYAML_is_sequence(local_nis))
3399 return LUSTRE_CFG_RC_BAD_PARAM;
3401 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
3402 intf = cYAML_get_object_item(item, "interfaces");
3405 num_entries = yaml_copy_intf_info(intf, &nw_descr);
3406 if (num_entries <= 0) {
3407 cYAML_build_error(num_entries, -1, "ni", "add",
3408 "bad interface list",
3410 return LUSTRE_CFG_RC_BAD_PARAM;
3414 seq_no = cYAML_get_object_item(tree, "seq_no");
3416 rc = lustre_lnet_del_ni((net) ? &nw_descr : NULL,
3417 (seq_no) ? seq_no->cy_valueint : -1,
3423 static int yaml_copy_peer_nids(struct cYAML *tree, char ***nidsppp, bool del)
3425 struct cYAML *nids_entry = NULL, *child = NULL, *entry = NULL,
3428 int num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
3430 prim_nid = cYAML_get_object_item(tree, "primary nid");
3431 if (!prim_nid || !prim_nid->cy_valuestring)
3432 return LUSTRE_CFG_RC_MISSING_PARAM;
3434 nids_entry = cYAML_get_object_item(tree, "peer ni");
3435 if (cYAML_is_sequence(nids_entry)) {
3436 while (cYAML_get_next_seq_item(nids_entry, &child)) {
3437 entry = cYAML_get_object_item(child, "nid");
3438 /* don't count an empty entry */
3439 if (!entry || !entry->cy_valuestring)
3442 if ((strcmp(entry->cy_valuestring, prim_nid->cy_valuestring)
3445 * primary nid is present in the list of
3446 * nids so that means we want to delete
3447 * the entire peer, so no need to go
3448 * further. Just delete the entire peer.
3458 return LUSTRE_CFG_RC_MISSING_PARAM;
3460 nids = calloc(sizeof(*nids) * num, 1);
3462 return LUSTRE_CFG_RC_OUT_OF_MEM;
3464 /* now grab all the nids */
3467 while (cYAML_get_next_seq_item(nids_entry, &child)) {
3468 entry = cYAML_get_object_item(child, "nid");
3469 if (!entry || !entry->cy_valuestring)
3472 nids[num] = calloc(strlen(entry->cy_valuestring) + 1, 1);
3474 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3477 strncpy(nids[num], entry->cy_valuestring,
3478 strlen(entry->cy_valuestring));
3488 yaml_free_string_array(nids, num);
3493 static int handle_yaml_config_peer(struct cYAML *tree, struct cYAML **show_rc,
3494 struct cYAML **err_rc)
3498 struct cYAML *seq_no, *prim_nid, *non_mr;
3500 num = yaml_copy_peer_nids(tree, &nids, false);
3504 seq_no = cYAML_get_object_item(tree, "seq_no");
3505 prim_nid = cYAML_get_object_item(tree, "primary nid");
3506 non_mr = cYAML_get_object_item(tree, "non_mr");
3508 rc = lustre_lnet_config_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
3510 (non_mr) ? false : true,
3511 (seq_no) ? seq_no->cy_valueint : -1,
3514 yaml_free_string_array(nids, num);
3518 static int handle_yaml_del_peer(struct cYAML *tree, struct cYAML **show_rc,
3519 struct cYAML **err_rc)
3523 struct cYAML *seq_no, *prim_nid;
3525 num = yaml_copy_peer_nids(tree, &nids, true);
3529 seq_no = cYAML_get_object_item(tree, "seq_no");
3530 prim_nid = cYAML_get_object_item(tree, "primary nid");
3532 rc = lustre_lnet_del_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
3534 (seq_no) ? seq_no->cy_valueint : -1,
3537 yaml_free_string_array(nids, num);
3541 static int handle_yaml_config_buffers(struct cYAML *tree,
3542 struct cYAML **show_rc,
3543 struct cYAML **err_rc)
3546 struct cYAML *tiny, *small, *large, *seq_no;
3548 tiny = cYAML_get_object_item(tree, "tiny");
3549 small = cYAML_get_object_item(tree, "small");
3550 large = cYAML_get_object_item(tree, "large");
3551 seq_no = cYAML_get_object_item(tree, "seq_no");
3553 rc = lustre_lnet_config_buffers((tiny) ? tiny->cy_valueint : -1,
3554 (small) ? small->cy_valueint : -1,
3555 (large) ? large->cy_valueint : -1,
3556 (seq_no) ? seq_no->cy_valueint : -1,
3562 static int handle_yaml_config_routing(struct cYAML *tree,
3563 struct cYAML **show_rc,
3564 struct cYAML **err_rc)
3566 int rc = LUSTRE_CFG_RC_NO_ERR;
3567 struct cYAML *seq_no, *enable;
3569 seq_no = cYAML_get_object_item(tree, "seq_no");
3570 enable = cYAML_get_object_item(tree, "enable");
3573 rc = lustre_lnet_enable_routing(enable->cy_valueint,
3575 seq_no->cy_valueint : -1,
3582 static int handle_yaml_del_route(struct cYAML *tree, struct cYAML **show_rc,
3583 struct cYAML **err_rc)
3587 struct cYAML *seq_no;
3589 net = cYAML_get_object_item(tree, "net");
3590 gw = cYAML_get_object_item(tree, "gateway");
3591 seq_no = cYAML_get_object_item(tree, "seq_no");
3593 return lustre_lnet_del_route((net) ? net->cy_valuestring : NULL,
3594 (gw) ? gw->cy_valuestring : NULL,
3595 (seq_no) ? seq_no->cy_valueint : -1,
3599 static int handle_yaml_del_routing(struct cYAML *tree, struct cYAML **show_rc,
3600 struct cYAML **err_rc)
3602 struct cYAML *seq_no;
3604 seq_no = cYAML_get_object_item(tree, "seq_no");
3606 return lustre_lnet_enable_routing(0, (seq_no) ?
3607 seq_no->cy_valueint : -1,
3611 static int handle_yaml_show_route(struct cYAML *tree, struct cYAML **show_rc,
3612 struct cYAML **err_rc)
3618 struct cYAML *detail;
3619 struct cYAML *seq_no;
3621 net = cYAML_get_object_item(tree, "net");
3622 gw = cYAML_get_object_item(tree, "gateway");
3623 hop = cYAML_get_object_item(tree, "hop");
3624 prio = cYAML_get_object_item(tree, "priority");
3625 detail = cYAML_get_object_item(tree, "detail");
3626 seq_no = cYAML_get_object_item(tree, "seq_no");
3628 return lustre_lnet_show_route((net) ? net->cy_valuestring : NULL,
3629 (gw) ? gw->cy_valuestring : NULL,
3630 (hop) ? hop->cy_valueint : -1,
3631 (prio) ? prio->cy_valueint : -1,
3632 (detail) ? detail->cy_valueint : 0,
3633 (seq_no) ? seq_no->cy_valueint : -1,
3638 static int handle_yaml_show_net(struct cYAML *tree, struct cYAML **show_rc,
3639 struct cYAML **err_rc)
3641 struct cYAML *net, *detail, *seq_no;
3643 net = cYAML_get_object_item(tree, "net");
3644 detail = cYAML_get_object_item(tree, "detail");
3645 seq_no = cYAML_get_object_item(tree, "seq_no");
3647 return lustre_lnet_show_net((net) ? net->cy_valuestring : NULL,
3648 (detail) ? detail->cy_valueint : 0,
3649 (seq_no) ? seq_no->cy_valueint : -1,
3654 static int handle_yaml_show_routing(struct cYAML *tree, struct cYAML **show_rc,
3655 struct cYAML **err_rc)
3657 struct cYAML *seq_no;
3659 seq_no = cYAML_get_object_item(tree, "seq_no");
3661 return lustre_lnet_show_routing((seq_no) ? seq_no->cy_valueint : -1,
3665 static int handle_yaml_show_peers(struct cYAML *tree, struct cYAML **show_rc,
3666 struct cYAML **err_rc)
3668 struct cYAML *seq_no, *nid, *detail;
3670 seq_no = cYAML_get_object_item(tree, "seq_no");
3671 detail = cYAML_get_object_item(tree, "detail");
3672 nid = cYAML_get_object_item(tree, "nid");
3674 return lustre_lnet_show_peer((nid) ? nid->cy_valuestring : NULL,
3675 (detail) ? detail->cy_valueint : 0,
3676 (seq_no) ? seq_no->cy_valueint : -1,
3680 static int handle_yaml_show_stats(struct cYAML *tree, struct cYAML **show_rc,
3681 struct cYAML **err_rc)
3683 struct cYAML *seq_no;
3685 seq_no = cYAML_get_object_item(tree, "seq_no");
3687 return lustre_lnet_show_stats((seq_no) ? seq_no->cy_valueint : -1,
3691 static int handle_yaml_config_numa(struct cYAML *tree, struct cYAML **show_rc,
3692 struct cYAML **err_rc)
3694 struct cYAML *seq_no, *range;
3696 seq_no = cYAML_get_object_item(tree, "seq_no");
3697 range = cYAML_get_object_item(tree, "range");
3699 return lustre_lnet_config_numa_range(range ? range->cy_valueint : -1,
3700 seq_no ? seq_no->cy_valueint : -1,
3704 static int handle_yaml_del_numa(struct cYAML *tree, struct cYAML **show_rc,
3705 struct cYAML **err_rc)
3707 struct cYAML *seq_no;
3709 seq_no = cYAML_get_object_item(tree, "seq_no");
3711 return lustre_lnet_config_numa_range(0, seq_no ? seq_no->cy_valueint : -1,
3715 static int handle_yaml_show_numa(struct cYAML *tree, struct cYAML **show_rc,
3716 struct cYAML **err_rc)
3718 struct cYAML *seq_no;
3720 seq_no = cYAML_get_object_item(tree, "seq_no");
3722 return lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint : -1,
3726 static int handle_yaml_config_global_settings(struct cYAML *tree,
3727 struct cYAML **show_rc,
3728 struct cYAML **err_rc)
3730 struct cYAML *max_intf, *numa, *discovery, *seq_no;
3733 seq_no = cYAML_get_object_item(tree, "seq_no");
3734 max_intf = cYAML_get_object_item(tree, "max_intf");
3736 rc = lustre_lnet_config_max_intf(max_intf->cy_valueint,
3737 seq_no ? seq_no->cy_valueint
3741 numa = cYAML_get_object_item(tree, "numa_range");
3743 rc = lustre_lnet_config_numa_range(numa->cy_valueint,
3744 seq_no ? seq_no->cy_valueint
3748 discovery = cYAML_get_object_item(tree, "discovery");
3750 rc = lustre_lnet_config_discovery(discovery->cy_valueint,
3751 seq_no ? seq_no->cy_valueint
3758 static int handle_yaml_del_global_settings(struct cYAML *tree,
3759 struct cYAML **show_rc,
3760 struct cYAML **err_rc)
3762 struct cYAML *max_intf, *numa, *discovery, *seq_no;
3765 seq_no = cYAML_get_object_item(tree, "seq_no");
3766 max_intf = cYAML_get_object_item(tree, "max_intf");
3768 rc = lustre_lnet_config_max_intf(LNET_INTERFACES_MAX_DEFAULT,
3769 seq_no ? seq_no->cy_valueint
3773 numa = cYAML_get_object_item(tree, "numa_range");
3775 rc = lustre_lnet_config_numa_range(0,
3776 seq_no ? seq_no->cy_valueint
3780 /* peer discovery is enabled by default */
3781 discovery = cYAML_get_object_item(tree, "discovery");
3783 rc = lustre_lnet_config_discovery(1,
3784 seq_no ? seq_no->cy_valueint
3791 static int handle_yaml_show_global_settings(struct cYAML *tree,
3792 struct cYAML **show_rc,
3793 struct cYAML **err_rc)
3795 struct cYAML *max_intf, *numa, *discovery, *seq_no;
3798 seq_no = cYAML_get_object_item(tree, "seq_no");
3799 max_intf = cYAML_get_object_item(tree, "max_intf");
3801 rc = lustre_lnet_show_max_intf(seq_no ? seq_no->cy_valueint
3805 numa = cYAML_get_object_item(tree, "numa_range");
3807 rc = lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint
3811 discovery = cYAML_get_object_item(tree, "discovery");
3813 rc = lustre_lnet_show_discovery(seq_no ? seq_no->cy_valueint
3820 static int handle_yaml_ping(struct cYAML *tree, struct cYAML **show_rc,
3821 struct cYAML **err_rc)
3823 struct cYAML *seq_no, *nid, *timeout;
3825 seq_no = cYAML_get_object_item(tree, "seq_no");
3826 nid = cYAML_get_object_item(tree, "primary nid");
3827 timeout = cYAML_get_object_item(tree, "timeout");
3829 return lustre_lnet_ping_nid((nid) ? nid->cy_valuestring : NULL,
3830 (timeout) ? timeout->cy_valueint : 1000,
3831 (seq_no) ? seq_no->cy_valueint : -1,
3835 static int handle_yaml_discover(struct cYAML *tree, struct cYAML **show_rc,
3836 struct cYAML **err_rc)
3838 struct cYAML *seq_no, *nid, *force;
3840 seq_no = cYAML_get_object_item(tree, "seq_no");
3841 nid = cYAML_get_object_item(tree, "primary nid");
3842 force = cYAML_get_object_item(tree, "force");
3844 return lustre_lnet_discover_nid((nid) ? nid->cy_valuestring : NULL,
3845 (force) ? force->cy_valueint : 0,
3846 (seq_no) ? seq_no->cy_valueint : -1,
3850 static int handle_yaml_no_op()
3852 return LUSTRE_CFG_RC_NO_ERR;
3855 struct lookup_cmd_hdlr_tbl {
3860 static struct lookup_cmd_hdlr_tbl lookup_config_tbl[] = {
3861 { .name = "route", .cb = handle_yaml_config_route },
3862 { .name = "net", .cb = handle_yaml_config_ni },
3863 { .name = "ip2nets", .cb = handle_yaml_config_ip2nets },
3864 { .name = "peer", .cb = handle_yaml_config_peer },
3865 { .name = "routing", .cb = handle_yaml_config_routing },
3866 { .name = "buffers", .cb = handle_yaml_config_buffers },
3867 { .name = "statistics", .cb = handle_yaml_no_op },
3868 { .name = "global", .cb = handle_yaml_config_global_settings},
3869 { .name = "numa", .cb = handle_yaml_config_numa },
3870 { .name = "ping", .cb = handle_yaml_no_op },
3871 { .name = "discover", .cb = handle_yaml_no_op },
3874 static struct lookup_cmd_hdlr_tbl lookup_del_tbl[] = {
3875 { .name = "route", .cb = handle_yaml_del_route },
3876 { .name = "net", .cb = handle_yaml_del_ni },
3877 { .name = "ip2nets", .cb = handle_yaml_no_op },
3878 { .name = "peer", .cb = handle_yaml_del_peer },
3879 { .name = "routing", .cb = handle_yaml_del_routing },
3880 { .name = "buffers", .cb = handle_yaml_no_op },
3881 { .name = "statistics", .cb = handle_yaml_no_op },
3882 { .name = "global", .cb = handle_yaml_del_global_settings},
3883 { .name = "numa", .cb = handle_yaml_del_numa },
3884 { .name = "ping", .cb = handle_yaml_no_op },
3885 { .name = "discover", .cb = handle_yaml_no_op },
3888 static struct lookup_cmd_hdlr_tbl lookup_show_tbl[] = {
3889 { .name = "route", .cb = handle_yaml_show_route },
3890 { .name = "net", .cb = handle_yaml_show_net },
3891 { .name = "peer", .cb = handle_yaml_show_peers },
3892 { .name = "ip2nets", .cb = handle_yaml_no_op },
3893 { .name = "routing", .cb = handle_yaml_show_routing },
3894 { .name = "buffers", .cb = handle_yaml_show_routing },
3895 { .name = "statistics", .cb = handle_yaml_show_stats },
3896 { .name = "global", .cb = handle_yaml_show_global_settings},
3897 { .name = "numa", .cb = handle_yaml_show_numa },
3898 { .name = "ping", .cb = handle_yaml_no_op },
3899 { .name = "discover", .cb = handle_yaml_no_op },
3902 static struct lookup_cmd_hdlr_tbl lookup_exec_tbl[] = {
3903 { .name = "route", .cb = handle_yaml_no_op },
3904 { .name = "net", .cb = handle_yaml_no_op },
3905 { .name = "peer", .cb = handle_yaml_no_op },
3906 { .name = "ip2nets", .cb = handle_yaml_no_op },
3907 { .name = "routing", .cb = handle_yaml_no_op },
3908 { .name = "buffers", .cb = handle_yaml_no_op },
3909 { .name = "statistics", .cb = handle_yaml_no_op },
3910 { .name = "global", .cb = handle_yaml_no_op },
3911 { .name = "numa", .cb = handle_yaml_no_op },
3912 { .name = "ping", .cb = handle_yaml_ping },
3913 { .name = "discover", .cb = handle_yaml_discover },
3916 static cmd_handler_t lookup_fn(char *key,
3917 struct lookup_cmd_hdlr_tbl *tbl)
3923 for (i = 0; tbl[i].name != NULL; i++) {
3924 if (strncmp(key, tbl[i].name, strlen(tbl[i].name)) == 0)
3931 static int lustre_yaml_cb_helper(char *f, struct lookup_cmd_hdlr_tbl *table,
3932 struct cYAML **show_rc, struct cYAML **err_rc)
3934 struct cYAML *tree, *item = NULL, *head, *child;
3936 char err_str[LNET_MAX_STR_LEN];
3937 int rc = LUSTRE_CFG_RC_NO_ERR, return_rc = LUSTRE_CFG_RC_NO_ERR;
3939 tree = cYAML_build_tree(f, NULL, 0, err_rc, false);
3941 return LUSTRE_CFG_RC_BAD_PARAM;
3943 child = tree->cy_child;
3944 while (child != NULL) {
3945 cb = lookup_fn(child->cy_string, table);
3947 snprintf(err_str, sizeof(err_str),
3948 "\"call back for '%s' not found\"",
3950 cYAML_build_error(LUSTRE_CFG_RC_BAD_PARAM, -1,
3951 "yaml", "helper", err_str, err_rc);
3955 if (cYAML_is_sequence(child)) {
3956 while ((head = cYAML_get_next_seq_item(child, &item))
3958 rc = cb(head, show_rc, err_rc);
3959 if (rc != LUSTRE_CFG_RC_NO_ERR)
3963 rc = cb(child, show_rc, err_rc);
3964 if (rc != LUSTRE_CFG_RC_NO_ERR)
3968 child = child->cy_next;
3972 cYAML_free_tree(tree);
3977 int lustre_yaml_config(char *f, struct cYAML **err_rc)
3979 return lustre_yaml_cb_helper(f, lookup_config_tbl,
3983 int lustre_yaml_del(char *f, struct cYAML **err_rc)
3985 return lustre_yaml_cb_helper(f, lookup_del_tbl,
3989 int lustre_yaml_show(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
3991 return lustre_yaml_cb_helper(f, lookup_show_tbl,
3995 int lustre_yaml_exec(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
3997 return lustre_yaml_cb_helper(f, lookup_exec_tbl,