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
43 #include <sys/ioctl.h>
45 #include <libcfs/util/ioctl.h>
46 #include <lnet/lnetctl.h>
47 #include <lnet/socklnd.h>
49 #include <lnet/lnet.h>
50 #include <sys/types.h>
52 #include "liblnetconfig.h"
55 #define CONFIG_CMD "configure"
56 #define UNCONFIG_CMD "unconfigure"
59 #define SHOW_CMD "show"
63 * lustre_lnet_ip_range_descr
64 * Describes an IP range.
65 * Each octect is an expression
67 struct lustre_lnet_ip_range_descr {
68 struct list_head ipr_entry;
69 struct list_head ipr_expr;
74 * Describes an ip2nets rule. This can be on a list of rules.
76 struct lustre_lnet_ip2nets {
77 struct lnet_dlc_network_descr ip2nets_net;
78 struct list_head ip2nets_ip_ranges;
83 * frees the memory allocated for an intf descriptor.
85 void free_intf_descr(struct lnet_dlc_intf_descr *intf_descr)
90 if (intf_descr->cpt_expr != NULL)
91 cfs_expr_list_free(intf_descr->cpt_expr);
96 * lustre_lnet_add_ip_range
98 * given a string of the format:
99 * <expr.expr.expr.expr> parse each expr into
100 * a lustre_lnet_ip_range_descr structure and insert on the list.
102 * This function is called from
103 * YAML on each ip-range.
104 * As a result of lnetctl command
105 * When building a NID or P2P selection rules
107 int lustre_lnet_add_ip_range(struct list_head *list, char *str_ip_range)
109 struct lustre_lnet_ip_range_descr *ip_range;
112 ip_range = calloc(1, sizeof(*ip_range));
113 if (ip_range == NULL)
114 return LUSTRE_CFG_RC_OUT_OF_MEM;
116 INIT_LIST_HEAD(&ip_range->ipr_entry);
117 INIT_LIST_HEAD(&ip_range->ipr_expr);
119 rc = cfs_ip_addr_parse(str_ip_range, strlen(str_ip_range),
120 &ip_range->ipr_expr);
122 return LUSTRE_CFG_RC_BAD_PARAM;
124 list_add_tail(&ip_range->ipr_entry, list);
126 return LUSTRE_CFG_RC_NO_ERR;
129 int lustre_lnet_add_intf_descr(struct list_head *list, char *intf, int len)
131 char *open_sq_bracket = NULL, *close_sq_bracket = NULL,
133 struct lnet_dlc_intf_descr *intf_descr = NULL;
135 char intf_string[LNET_MAX_STR_LEN];
137 if (len >= LNET_MAX_STR_LEN)
138 return LUSTRE_CFG_RC_BAD_PARAM;
140 strncpy(intf_string, intf, len);
141 intf_string[len] = '\0';
143 intf_descr = calloc(1, sizeof(*intf_descr));
144 if (intf_descr == NULL)
145 return LUSTRE_CFG_RC_OUT_OF_MEM;
147 INIT_LIST_HEAD(&intf_descr->intf_on_network);
149 intf_name = intf_string;
150 open_sq_bracket = strchr(intf_string, '[');
151 if (open_sq_bracket != NULL) {
152 close_sq_bracket = strchr(intf_string, ']');
153 if (close_sq_bracket == NULL) {
155 return LUSTRE_CFG_RC_BAD_PARAM;
157 rc = cfs_expr_list_parse(open_sq_bracket,
158 strlen(open_sq_bracket), 0, UINT_MAX,
159 &intf_descr->cpt_expr);
162 return LUSTRE_CFG_RC_BAD_PARAM;
164 strncpy(intf_descr->intf_name, intf_name,
165 open_sq_bracket - intf_name);
166 intf_descr->intf_name[open_sq_bracket - intf_name] = '\0';
168 strcpy(intf_descr->intf_name, intf_name);
169 intf_descr->cpt_expr = NULL;
172 list_add_tail(&intf_descr->intf_on_network, list);
174 return LUSTRE_CFG_RC_NO_ERR;
177 void lustre_lnet_init_nw_descr(struct lnet_dlc_network_descr *nw_descr)
179 if (nw_descr != NULL) {
180 INIT_LIST_HEAD(&nw_descr->network_on_rule);
181 INIT_LIST_HEAD(&nw_descr->nw_intflist);
185 int lustre_lnet_parse_nids(char *nids, char **array, int size,
189 char *comma = nids, *cur, *entry;
191 int i, len, start = 0, finish = 0;
193 if (nids == NULL || strlen(nids) == 0)
196 /* count the number or new nids, by counting the number of commas */
198 comma = strchr(comma, ',');
208 * if the array is not NULL allocate a large enough array to house
209 * the old and new entries
211 new_array = calloc(sizeof(char*),
212 (size > 0) ? size + num_nids : num_nids);
217 /* parse our the new nids and add them to the tail of the array */
220 start = (size > 0) ? size: 0;
221 finish = (size > 0) ? size + num_nids : num_nids;
222 for (i = start; i < finish; i++) {
223 comma = strchr(comma, ',');
226 * the length of the string to be parsed out is
227 * from cur to end of string. So it's good enough
230 len = strlen(cur) + 1;
232 /* length of the string is comma - cur */
233 len = (comma - cur) + 1;
235 entry = calloc(1, len);
237 finish = i > 0 ? i - 1: 0;
240 strncpy(entry, cur, len - 1);
242 new_array[i] = entry;
249 /* add the old entries in the array and delete the old array*/
250 for (i = 0; i < size; i++)
251 new_array[i] = array[i];
256 *out_array = new_array;
261 for (i = start; i < finish; i++)
271 * <intf>[<expr>], <intf>[<expr>],..
273 int lustre_lnet_parse_interfaces(char *intf_str,
274 struct lnet_dlc_network_descr *nw_descr)
279 char *cur = intf_str, *next = NULL;
280 char *end = intf_str + strlen(intf_str);
282 struct lnet_dlc_intf_descr *intf_descr, *tmp;
284 if (nw_descr == NULL)
285 return LUSTRE_CFG_RC_BAD_PARAM;
288 open_square = strchr(cur, '[');
289 if (open_square != NULL) {
290 close_square = strchr(cur, ']');
291 if (close_square == NULL) {
292 rc = LUSTRE_CFG_RC_BAD_PARAM;
296 comma = strchr(cur, ',');
297 if (comma != NULL && comma > close_square) {
299 len = next - close_square;
305 comma = strchr(cur, ',');
315 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist, cur, len);
316 if (rc != LUSTRE_CFG_RC_NO_ERR)
322 return LUSTRE_CFG_RC_NO_ERR;
325 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
327 list_del(&intf_descr->intf_on_network);
328 free_intf_descr(intf_descr);
334 int lustre_lnet_config_lib_init(void)
336 return register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH,
337 LNET_DEV_MAJOR, LNET_DEV_MINOR);
340 void lustre_lnet_config_lib_uninit(void)
342 unregister_ioc_dev(LNET_DEV_ID);
345 int lustre_lnet_config_ni_system(bool up, bool load_ni_from_mod,
346 int seq_no, struct cYAML **err_rc)
348 struct libcfs_ioctl_data data;
351 char err_str[LNET_MAX_STR_LEN];
353 snprintf(err_str, sizeof(err_str), "\"Success\"");
355 LIBCFS_IOC_INIT(data);
357 /* Reverse logic is used here in order not to change
358 * the lctl utility */
359 data.ioc_flags = load_ni_from_mod ? 0 : 1;
361 opc = up ? IOC_LIBCFS_CONFIGURE : IOC_LIBCFS_UNCONFIGURE;
363 rc = l_ioctl(LNET_DEV_ID, opc, &data);
367 "\"LNet %s error: %s\"", (up) ? "configure" :
368 "unconfigure", strerror(errno));
372 cYAML_build_error(rc, seq_no, (up) ? CONFIG_CMD : UNCONFIG_CMD,
373 "lnet", err_str, err_rc);
378 static lnet_nid_t *allocate_create_nid_array(char **nids, __u32 num_nids,
381 lnet_nid_t *array = NULL;
385 snprintf(err_str, LNET_MAX_STR_LEN, "no NIDs to add");
389 array = calloc(sizeof(*array) * num_nids, 1);
391 snprintf(err_str, LNET_MAX_STR_LEN, "out of memory");
395 for (i = 0; i < num_nids; i++) {
396 array[i] = libcfs_str2nid(nids[i]);
397 if (array[i] == LNET_NID_ANY) {
399 snprintf(err_str, LNET_MAX_STR_LEN,
409 static int dispatch_peer_ni_cmd(lnet_nid_t pnid, lnet_nid_t nid, __u32 cmd,
410 struct lnet_ioctl_peer_cfg *data,
411 char *err_str, char *cmd_str)
415 data->prcfg_prim_nid = pnid;
416 data->prcfg_cfg_nid = nid;
418 rc = l_ioctl(LNET_DEV_ID, cmd, data);
423 "\"cannot %s peer ni: %s\"",
424 (cmd_str) ? cmd_str : "add", strerror(errno));
430 int lustre_lnet_config_peer_nid(char *pnid, char **nid, int num_nids,
431 bool mr, int seq_no, struct cYAML **err_rc)
433 struct lnet_ioctl_peer_cfg data;
434 lnet_nid_t prim_nid = LNET_NID_ANY;
435 int rc = LUSTRE_CFG_RC_NO_ERR;
437 bool nid0_used = false;
438 char err_str[LNET_MAX_STR_LEN] = {0};
439 lnet_nid_t *nids = allocate_create_nid_array(nid, num_nids, err_str);
442 prim_nid = libcfs_str2nid(pnid);
443 if (prim_nid == LNET_NID_ANY) {
444 snprintf(err_str, sizeof(err_str),
447 rc = LUSTRE_CFG_RC_MISSING_PARAM;
450 } else if (!nids || nids[0] == LNET_NID_ANY) {
451 snprintf(err_str, sizeof(err_str),
452 "no NIDs provided for configuration");
453 rc = LUSTRE_CFG_RC_MISSING_PARAM;
456 prim_nid = LNET_NID_ANY;
459 snprintf(err_str, sizeof(err_str), "\"Success\"");
461 LIBCFS_IOC_INIT_V2(data, prcfg_hdr);
465 * if prim_nid is not specified use the first nid in the list of
466 * nids provided as the prim_nid. NOTE: on entering 'if' we must
467 * have at least 1 NID
469 if (prim_nid == LNET_NID_ANY) {
474 /* Create the prim_nid first */
475 rc = dispatch_peer_ni_cmd(prim_nid, LNET_NID_ANY,
476 IOC_LIBCFS_ADD_PEER_NI,
477 &data, err_str, "add");
482 /* add the rest of the nids to the key nid if any are available */
483 for (idx = nid0_used ? 1 : 0 ; nids && idx < num_nids; idx++) {
485 * If prim_nid is not provided then the first nid in the
486 * list becomes the prim_nid. First time round the loop use
487 * LNET_NID_ANY for the first parameter, then use nid[0]
488 * as the key nid after wards
490 rc = dispatch_peer_ni_cmd(prim_nid, nids[idx],
491 IOC_LIBCFS_ADD_PEER_NI, &data,
501 cYAML_build_error(rc, seq_no, ADD_CMD, "peer_ni", err_str, err_rc);
505 int lustre_lnet_del_peer_nid(char *pnid, char **nid, int num_nids,
506 int seq_no, struct cYAML **err_rc)
508 struct lnet_ioctl_peer_cfg data;
510 int rc = LUSTRE_CFG_RC_NO_ERR;
512 char err_str[LNET_MAX_STR_LEN] = {0};
513 lnet_nid_t *nids = allocate_create_nid_array(nid, num_nids, err_str);
516 snprintf(err_str, sizeof(err_str),
517 "\"Primary nid is not provided\"");
518 rc = LUSTRE_CFG_RC_MISSING_PARAM;
521 prim_nid = libcfs_str2nid(pnid);
522 if (prim_nid == LNET_NID_ANY) {
523 rc = LUSTRE_CFG_RC_BAD_PARAM;
524 snprintf(err_str, sizeof(err_str),
531 snprintf(err_str, sizeof(err_str), "\"Success\"");
533 LIBCFS_IOC_INIT_V2(data, prcfg_hdr);
534 if (!nids || nids[0] == LNET_NID_ANY) {
535 rc = dispatch_peer_ni_cmd(prim_nid, LNET_NID_ANY,
536 IOC_LIBCFS_DEL_PEER_NI,
537 &data, err_str, "del");
541 for (idx = 0; nids && idx < num_nids; idx++) {
542 rc = dispatch_peer_ni_cmd(prim_nid, nids[idx],
543 IOC_LIBCFS_DEL_PEER_NI, &data,
553 cYAML_build_error(rc, seq_no, DEL_CMD, "peer_ni", err_str, err_rc);
557 int lustre_lnet_config_route(char *nw, char *gw, int hops, int prio,
558 int seq_no, struct cYAML **err_rc)
560 struct lnet_ioctl_config_data data;
561 lnet_nid_t gateway_nid;
562 int rc = LUSTRE_CFG_RC_NO_ERR;
563 __u32 net = LNET_NIDNET(LNET_NID_ANY);
564 char err_str[LNET_MAX_STR_LEN];
566 snprintf(err_str, sizeof(err_str), "\"Success\"");
568 if (nw == NULL || gw == NULL) {
571 "\"missing mandatory parameter(s): '%s'\"",
572 (nw == NULL && gw == NULL) ? "network, gateway" :
573 (nw == NULL) ? "network" : "gateway");
574 rc = LUSTRE_CFG_RC_MISSING_PARAM;
578 net = libcfs_str2net(nw);
579 if (net == LNET_NIDNET(LNET_NID_ANY)) {
582 "\"cannot parse net %s\"", nw);
583 rc = LUSTRE_CFG_RC_BAD_PARAM;
587 gateway_nid = libcfs_str2nid(gw);
588 if (gateway_nid == LNET_NID_ANY) {
591 "\"cannot parse gateway NID '%s'\"", gw);
592 rc = LUSTRE_CFG_RC_BAD_PARAM;
597 /* hops is undefined */
598 hops = LNET_UNDEFINED_HOPS;
599 } else if (hops < 1 || hops > 255) {
602 "\"invalid hop count %d, must be between 1 and 255\"",
604 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
610 } else if (prio < 0) {
613 "\"invalid priority %d, must be greater than 0\"",
615 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
619 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
621 data.cfg_config_u.cfg_route.rtr_hop = hops;
622 data.cfg_config_u.cfg_route.rtr_priority = prio;
623 data.cfg_nid = gateway_nid;
625 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
630 "\"cannot add route: %s\"", strerror(errno));
635 cYAML_build_error(rc, seq_no, ADD_CMD, "route", err_str, err_rc);
640 int lustre_lnet_del_route(char *nw, char *gw,
641 int seq_no, struct cYAML **err_rc)
643 struct lnet_ioctl_config_data data;
644 lnet_nid_t gateway_nid;
645 int rc = LUSTRE_CFG_RC_NO_ERR;
646 __u32 net = LNET_NIDNET(LNET_NID_ANY);
647 char err_str[LNET_MAX_STR_LEN];
649 snprintf(err_str, sizeof(err_str), "\"Success\"");
651 if (nw == NULL || gw == NULL) {
654 "\"missing mandatory parameter(s): '%s'\"",
655 (nw == NULL && gw == NULL) ? "network, gateway" :
656 (nw == NULL) ? "network" : "gateway");
657 rc = LUSTRE_CFG_RC_MISSING_PARAM;
661 net = libcfs_str2net(nw);
662 if (net == LNET_NIDNET(LNET_NID_ANY)) {
665 "\"cannot parse net '%s'\"", nw);
666 rc = LUSTRE_CFG_RC_BAD_PARAM;
670 gateway_nid = libcfs_str2nid(gw);
671 if (gateway_nid == LNET_NID_ANY) {
674 "\"cannot parse gateway NID '%s'\"", gw);
675 rc = LUSTRE_CFG_RC_BAD_PARAM;
679 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
681 data.cfg_nid = gateway_nid;
683 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
688 "\"cannot delete route: %s\"", strerror(errno));
693 cYAML_build_error(rc, seq_no, DEL_CMD, "route", err_str, err_rc);
698 int lustre_lnet_show_route(char *nw, char *gw, int hops, int prio, int detail,
699 int seq_no, struct cYAML **show_rc,
700 struct cYAML **err_rc)
702 struct lnet_ioctl_config_data data;
703 lnet_nid_t gateway_nid;
704 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
706 __u32 net = LNET_NIDNET(LNET_NID_ANY);
708 struct cYAML *root = NULL, *route = NULL, *item = NULL;
709 struct cYAML *first_seq = NULL;
710 char err_str[LNET_MAX_STR_LEN];
713 snprintf(err_str, sizeof(err_str),
714 "\"out of memory\"");
717 net = libcfs_str2net(nw);
718 if (net == LNET_NIDNET(LNET_NID_ANY)) {
721 "\"cannot parse net '%s'\"", nw);
722 rc = LUSTRE_CFG_RC_BAD_PARAM;
727 /* show all routes without filtering on net */
728 net = LNET_NIDNET(LNET_NID_ANY);
732 gateway_nid = libcfs_str2nid(gw);
733 if (gateway_nid == LNET_NID_ANY) {
736 "\"cannot parse gateway NID '%s'\"", gw);
737 rc = LUSTRE_CFG_RC_BAD_PARAM;
741 /* show all routes with out filtering on gateway */
742 gateway_nid = LNET_NID_ANY;
744 if ((hops < 1 && hops != -1) || hops > 255) {
747 "\"invalid hop count %d, must be between 0 and 256\"",
749 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
753 /* create struct cYAML root object */
754 root = cYAML_create_object(NULL, NULL);
758 route = cYAML_create_seq(root, "route");
763 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
766 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
772 /* filter on provided data */
773 if (net != LNET_NIDNET(LNET_NID_ANY) &&
777 if (gateway_nid != LNET_NID_ANY &&
778 gateway_nid != data.cfg_nid)
782 hops != data.cfg_config_u.cfg_route.rtr_hop)
786 prio != data.cfg_config_u.cfg_route.rtr_priority)
789 /* default rc to -1 incase we hit the goto */
793 item = cYAML_create_seq_item(route);
797 if (first_seq == NULL)
800 if (cYAML_create_string(item, "net",
801 libcfs_net2str(data.cfg_net)) == NULL)
804 if (cYAML_create_string(item, "gateway",
805 libcfs_nid2str(data.cfg_nid)) == NULL)
809 if (cYAML_create_number(item, "hop",
810 data.cfg_config_u.cfg_route.
815 if (cYAML_create_number(item, "priority",
817 cfg_route.rtr_priority) == NULL)
820 if (cYAML_create_string(item, "state",
821 data.cfg_config_u.cfg_route.
823 "up" : "down") == NULL)
828 /* print output iff show_rc is not provided */
830 cYAML_print_tree(root);
832 if (l_errno != ENOENT) {
835 "\"cannot get routes: %s\"",
840 rc = LUSTRE_CFG_RC_NO_ERR;
842 snprintf(err_str, sizeof(err_str), "\"success\"");
844 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
845 cYAML_free_tree(root);
846 } else if (show_rc != NULL && *show_rc != NULL) {
847 struct cYAML *show_node;
848 /* find the route node, if one doesn't exist then
849 * insert one. Otherwise add to the one there
851 show_node = cYAML_get_object_item(*show_rc, "route");
852 if (show_node != NULL && cYAML_is_sequence(show_node)) {
853 cYAML_insert_child(show_node, first_seq);
856 } else if (show_node == NULL) {
857 cYAML_insert_sibling((*show_rc)->cy_child,
861 cYAML_free_tree(root);
867 cYAML_build_error(rc, seq_no, SHOW_CMD, "route", err_str, err_rc);
872 static int socket_intf_query(int request, char *intf,
878 if (strlen(intf) >= IFNAMSIZ || ifr == NULL)
879 return LUSTRE_CFG_RC_BAD_PARAM;
881 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
883 return LUSTRE_CFG_RC_BAD_PARAM;
885 strcpy(ifr->ifr_name, intf);
886 rc = ioctl(sockfd, request, ifr);
888 return LUSTRE_CFG_RC_BAD_PARAM;
894 * for each interface in the array of interfaces find the IP address of
895 * that interface, create its nid and add it to an array of NIDs.
896 * Stop if any of the interfaces is down
898 static int lustre_lnet_intf2nids(struct lnet_dlc_network_descr *nw,
899 lnet_nid_t **nids, __u32 *nnids)
901 int i = 0, count = 0, rc;
904 struct lnet_dlc_intf_descr *intf;
906 if (nw == NULL || nids == NULL)
907 return LUSTRE_CFG_RC_BAD_PARAM;
909 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network)
912 *nids = calloc(count, sizeof(lnet_nid_t));
914 return LUSTRE_CFG_RC_OUT_OF_MEM;
916 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network) {
917 memset(&ifr, 0, sizeof(ifr));
918 rc = socket_intf_query(SIOCGIFFLAGS, intf->intf_name, &ifr);
922 if ((ifr.ifr_flags & IFF_UP) == 0) {
923 rc = LUSTRE_CFG_RC_BAD_PARAM;
927 memset(&ifr, 0, sizeof(ifr));
928 rc = socket_intf_query(SIOCGIFADDR, intf->intf_name, &ifr);
932 ip = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
934 (*nids)[i] = LNET_MKNID(nw->nw_id, ip);
949 * called repeatedly until a match or no more ip range
951 * ip_range expression
952 * interface list with all the interface names.
953 * all the interfaces in the system.
955 * try to match the ip_range expr to one of the interfaces' IPs in
956 * the system. If we hit a patch for an interface. Check if that
957 * interface name is in the list.
959 * If there are more than one interface in the list, then make sure
960 * that the IPs for all of these interfaces match the ip ranges
963 * for each interface in intf_list
964 * look up the intf name in ifa
965 * if not there then no match
966 * check ip obtained from ifa against a match to any of the
968 * If no match, then fail
970 * The result is that all the interfaces have to match.
972 int lustre_lnet_match_ip_to_intf(struct ifaddrs *ifa,
973 struct list_head *intf_list,
974 struct list_head *ip_ranges)
978 struct lnet_dlc_intf_descr *intf_descr, *tmp;
979 struct ifaddrs *ifaddr = ifa;
980 struct lustre_lnet_ip_range_descr *ip_range;
984 * if there are no explicit interfaces, and no ip ranges, then
985 * configure the first tcp interface we encounter.
987 if (list_empty(intf_list) && list_empty(ip_ranges)) {
988 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
989 if (ifaddr->ifa_addr == NULL)
992 if ((ifaddr->ifa_flags & IFF_UP) == 0)
995 family = ifaddr->ifa_addr->sa_family;
996 if (family == AF_INET &&
997 strcmp(ifaddr->ifa_name, "lo") != 0) {
998 rc = lustre_lnet_add_intf_descr
999 (intf_list, ifaddr->ifa_name,
1000 strlen(ifaddr->ifa_name));
1002 if (rc != LUSTRE_CFG_RC_NO_ERR)
1005 return LUSTRE_CFG_RC_MATCH;
1008 return LUSTRE_CFG_RC_NO_MATCH;
1012 * First interface which matches an IP pattern will be used
1014 if (list_empty(intf_list)) {
1016 * no interfaces provided in the rule, but an ip range is
1017 * provided, so try and match an interface to the ip
1020 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1021 if (ifaddr->ifa_addr == NULL)
1024 if ((ifaddr->ifa_flags & IFF_UP) == 0)
1027 family = ifaddr->ifa_addr->sa_family;
1028 if (family == AF_INET) {
1029 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->
1032 list_for_each_entry(ip_range, ip_ranges,
1034 rc = cfs_ip_addr_match(bswap_32(ip),
1035 &ip_range->ipr_expr);
1039 rc = lustre_lnet_add_intf_descr
1040 (intf_list, ifaddr->ifa_name,
1041 strlen(ifaddr->ifa_name));
1043 if (rc != LUSTRE_CFG_RC_NO_ERR)
1049 if (!list_empty(intf_list))
1050 return LUSTRE_CFG_RC_MATCH;
1052 return LUSTRE_CFG_RC_NO_MATCH;
1056 * If an interface is explicitly specified the ip-range might or
1057 * might not be specified. if specified the interface needs to match the
1058 * ip-range. If no ip-range then the interfaces are
1059 * automatically matched if they are all up.
1060 * If > 1 interfaces all the interfaces must match for the NI to
1063 list_for_each_entry_safe(intf_descr, tmp, intf_list, intf_on_network) {
1064 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1065 if (ifaddr->ifa_addr == NULL)
1068 family = ifaddr->ifa_addr->sa_family;
1069 if (family == AF_INET &&
1070 strcmp(intf_descr->intf_name,
1071 ifaddr->ifa_name) == 0)
1075 if (ifaddr == NULL) {
1076 list_del(&intf_descr->intf_on_network);
1077 free_intf_descr(intf_descr);
1081 if ((ifaddr->ifa_flags & IFF_UP) == 0) {
1082 list_del(&intf_descr->intf_on_network);
1083 free_intf_descr(intf_descr);
1087 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr.s_addr;
1090 list_for_each_entry(ip_range, ip_ranges, ipr_entry) {
1091 rc = cfs_ip_addr_match(bswap_32(ip), &ip_range->ipr_expr);
1097 /* no match for this interface */
1098 list_del(&intf_descr->intf_on_network);
1099 free_intf_descr(intf_descr);
1103 return LUSTRE_CFG_RC_MATCH;
1106 int lustre_lnet_resolve_ip2nets_rule(struct lustre_lnet_ip2nets *ip2nets,
1107 lnet_nid_t **nids, __u32 *nnids)
1109 struct ifaddrs *ifa;
1110 int rc = LUSTRE_CFG_RC_NO_ERR;
1112 rc = getifaddrs(&ifa);
1116 rc = lustre_lnet_match_ip_to_intf(ifa,
1117 &ip2nets->ip2nets_net.nw_intflist,
1118 &ip2nets->ip2nets_ip_ranges);
1119 if (rc != LUSTRE_CFG_RC_MATCH) {
1124 rc = lustre_lnet_intf2nids(&ip2nets->ip2nets_net, nids, nnids);
1125 if (rc != LUSTRE_CFG_RC_NO_ERR) {
1136 lustre_lnet_ioctl_config_ni(struct list_head *intf_list,
1137 struct lnet_ioctl_config_lnd_tunables *tunables,
1138 struct cfs_expr_list *global_cpts,
1139 lnet_nid_t *nids, char *err_str)
1142 struct lnet_ioctl_config_ni *conf;
1143 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1144 int rc = LUSTRE_CFG_RC_NO_ERR, i = 0;
1147 struct lnet_dlc_intf_descr *intf_descr;
1149 struct cfs_expr_list *cpt_expr;
1151 list_for_each_entry(intf_descr, intf_list,
1153 if (i == 0 && tunables != NULL)
1154 len = sizeof(struct lnet_ioctl_config_ni) +
1155 sizeof(struct lnet_ioctl_config_lnd_tunables);
1157 len = sizeof(struct lnet_ioctl_config_ni);
1159 data = calloc(1, len);
1160 conf = (struct lnet_ioctl_config_ni*) data;
1161 if (i == 0 && tunables != NULL)
1162 tun = (struct lnet_ioctl_config_lnd_tunables*)
1165 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1166 conf->lic_cfg_hdr.ioc_len = len;
1167 conf->lic_nid = nids[i];
1168 strncpy(conf->lic_ni_intf[0], intf_descr->intf_name,
1171 if (intf_descr->cpt_expr != NULL)
1172 cpt_expr = intf_descr->cpt_expr;
1173 else if (global_cpts != NULL)
1174 cpt_expr = global_cpts;
1178 if (cpt_expr != NULL) {
1179 count = cfs_expr_list_values(cpt_expr,
1180 LNET_MAX_SHOW_NUM_CPT,
1183 memcpy(conf->lic_cpts, cpt_array,
1184 sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1193 conf->lic_ncpts = count;
1195 if (i == 0 && tunables != NULL)
1196 /* TODO put in the LND tunables */
1197 memcpy(tun, tunables, sizeof(*tunables));
1199 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1204 "\"cannot add network: %s\"", strerror(errno));
1210 return LUSTRE_CFG_RC_NO_ERR;
1214 lustre_lnet_config_ip2nets(struct lustre_lnet_ip2nets *ip2nets,
1215 struct lnet_ioctl_config_lnd_tunables *tunables,
1216 struct cfs_expr_list *global_cpts,
1217 int seq_no, struct cYAML **err_rc)
1219 lnet_nid_t *nids = NULL;
1222 char err_str[LNET_MAX_STR_LEN];
1224 snprintf(err_str, sizeof(err_str), "\"success\"");
1229 "\"incomplete ip2nets information\"");
1230 rc = LUSTRE_CFG_RC_BAD_PARAM;
1234 rc = lustre_lnet_resolve_ip2nets_rule(ip2nets, &nids, &nnids);
1235 if (rc != LUSTRE_CFG_RC_NO_ERR && rc != LUSTRE_CFG_RC_MATCH) {
1238 "\"cannot resolve ip2nets rule\"");
1242 if (list_empty(&ip2nets->ip2nets_net.nw_intflist)) {
1243 snprintf(err_str, sizeof(err_str),
1244 "\"no interfaces match ip2nets rules\"");
1248 rc = lustre_lnet_ioctl_config_ni(&ip2nets->ip2nets_net.nw_intflist,
1249 tunables, global_cpts, nids,
1251 if (rc != LUSTRE_CFG_RC_NO_ERR)
1255 cYAML_build_error(rc, seq_no, ADD_CMD, "ip2nets", err_str, err_rc);
1259 int lustre_lnet_config_ni(struct lnet_dlc_network_descr *nw_descr,
1260 struct cfs_expr_list *global_cpts,
1262 struct lnet_ioctl_config_lnd_tunables *tunables,
1263 int seq_no, struct cYAML **err_rc)
1266 struct lnet_ioctl_config_ni *conf;
1267 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1268 char buf[LNET_MAX_STR_LEN];
1269 int rc = LUSTRE_CFG_RC_NO_ERR;
1270 char err_str[LNET_MAX_STR_LEN];
1271 lnet_nid_t *nids = NULL;
1275 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1278 snprintf(err_str, sizeof(err_str), "\"success\"");
1280 if (ip2net == NULL && nw_descr == NULL) {
1283 "\"mandatory parameters not specified.\"");
1284 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1288 if (ip2net != NULL && strlen(ip2net) >= sizeof(buf)) {
1291 "\"ip2net string too long %d\"",
1292 (int)strlen(ip2net));
1293 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1297 if (ip2net != NULL) {
1298 if (tunables != NULL)
1299 len = sizeof(struct lnet_ioctl_config_ni) +
1300 sizeof(struct lnet_ioctl_config_lnd_tunables);
1302 len = sizeof(struct lnet_ioctl_config_ni);
1303 data = calloc(1, len);
1304 conf = (struct lnet_ioctl_config_ni*) data;
1305 if (tunables != NULL)
1306 tun = (struct lnet_ioctl_config_lnd_tunables*)
1307 (data + sizeof(*conf));
1309 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1310 conf->lic_cfg_hdr.ioc_len = len;
1311 strncpy(conf->lic_legacy_ip2nets, ip2net,
1314 if (global_cpts != NULL) {
1315 count = cfs_expr_list_values(global_cpts,
1316 LNET_MAX_SHOW_NUM_CPT,
1319 memcpy(conf->lic_cpts, cpt_array,
1320 sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1329 conf->lic_ncpts = count;
1331 if (tunables != NULL)
1332 memcpy(tun, tunables, sizeof(*tunables));
1334 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1339 "\"cannot add network: %s\"", strerror(errno));
1346 if (LNET_NETTYP(nw_descr->nw_id) == LOLND)
1347 return LUSTRE_CFG_RC_NO_ERR;
1349 if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1352 "\"cannot parse net '%s'\"",
1353 libcfs_net2str(nw_descr->nw_id));
1354 rc = LUSTRE_CFG_RC_BAD_PARAM;
1358 if (list_empty(&nw_descr->nw_intflist)) {
1361 "\"no interface name provided\"");
1362 rc = LUSTRE_CFG_RC_BAD_PARAM;
1366 rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids);
1368 snprintf(err_str, sizeof(err_str),
1369 "\"bad parameter\"");
1370 rc = LUSTRE_CFG_RC_BAD_PARAM;
1374 rc = lustre_lnet_ioctl_config_ni(&nw_descr->nw_intflist,
1375 tunables, global_cpts, nids,
1379 if (nw_descr != NULL) {
1380 list_for_each_entry_safe(intf_descr, tmp,
1381 &nw_descr->nw_intflist,
1383 list_del(&intf_descr->intf_on_network);
1384 free_intf_descr(intf_descr);
1388 cYAML_build_error(rc, seq_no, ADD_CMD, "net", err_str, err_rc);
1399 int lustre_lnet_del_ni(struct lnet_dlc_network_descr *nw_descr,
1400 int seq_no, struct cYAML **err_rc)
1402 struct lnet_ioctl_config_ni data;
1403 int rc = LUSTRE_CFG_RC_NO_ERR, i;
1404 char err_str[LNET_MAX_STR_LEN];
1405 lnet_nid_t *nids = NULL;
1407 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1409 if (LNET_NETTYP(nw_descr->nw_id) == LOLND)
1410 return LUSTRE_CFG_RC_NO_ERR;
1412 snprintf(err_str, sizeof(err_str), "\"success\"");
1414 if (nw_descr == NULL) {
1417 "\"missing mandatory parameter\"");
1418 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1422 if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1425 "\"cannot parse net '%s'\"",
1426 libcfs_net2str(nw_descr->nw_id));
1427 rc = LUSTRE_CFG_RC_BAD_PARAM;
1431 rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids);
1433 snprintf(err_str, sizeof(err_str),
1434 "\"bad parameter\"");
1435 rc = LUSTRE_CFG_RC_BAD_PARAM;
1440 * no interfaces just the nw_id is specified
1443 nids = calloc(1, sizeof(*nids));
1445 snprintf(err_str, sizeof(err_str),
1446 "\"out of memory\"");
1447 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1450 nids[0] = LNET_MKNID(nw_descr->nw_id, 0);
1454 for (i = 0; i < nnids; i++) {
1455 LIBCFS_IOC_INIT_V2(data, lic_cfg_hdr);
1456 data.lic_nid = nids[i];
1458 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_LOCAL_NI, &data);
1463 "\"cannot del network: %s\"", strerror(errno));
1467 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
1469 list_del(&intf_descr->intf_on_network);
1470 free_intf_descr(intf_descr);
1474 cYAML_build_error(rc, seq_no, DEL_CMD, "net", err_str, err_rc);
1482 int lustre_lnet_show_net(char *nw, int detail, int seq_no,
1483 struct cYAML **show_rc, struct cYAML **err_rc)
1486 struct lnet_ioctl_config_ni *ni_data;
1487 struct lnet_ioctl_config_lnd_tunables *lnd;
1488 struct lnet_ioctl_element_stats *stats;
1489 __u32 net = LNET_NIDNET(LNET_NID_ANY);
1490 __u32 prev_net = LNET_NIDNET(LNET_NID_ANY);
1491 int rc = LUSTRE_CFG_RC_OUT_OF_MEM, i, j;
1493 struct cYAML *root = NULL, *tunables = NULL,
1494 *net_node = NULL, *interfaces = NULL,
1495 *item = NULL, *first_seq = NULL,
1496 *tmp = NULL, *statistics = NULL;
1497 int str_buf_len = LNET_MAX_SHOW_NUM_CPT * 2;
1498 char str_buf[str_buf_len];
1500 char err_str[LNET_MAX_STR_LEN];
1501 bool exist = false, new_net = true;
1503 size_t buf_size = sizeof(*ni_data) + sizeof(*lnd) + sizeof(*stats);
1505 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1507 buf = calloc(1, buf_size);
1511 ni_data = (struct lnet_ioctl_config_ni *)buf;
1514 net = libcfs_str2net(nw);
1515 if (net == LNET_NIDNET(LNET_NID_ANY)) {
1518 "\"cannot parse net '%s'\"", nw);
1519 rc = LUSTRE_CFG_RC_BAD_PARAM;
1524 root = cYAML_create_object(NULL, NULL);
1528 net_node = cYAML_create_seq(root, "net");
1529 if (net_node == NULL)
1536 memset(buf, 0, buf_size);
1538 LIBCFS_IOC_INIT_V2(*ni_data, lic_cfg_hdr);
1540 * set the ioc_len to the proper value since INIT assumes
1543 ni_data->lic_cfg_hdr.ioc_len = buf_size;
1544 ni_data->lic_idx = i;
1546 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LOCAL_NI, ni_data);
1552 rc_net = LNET_NIDNET(ni_data->lic_nid);
1554 /* filter on provided data */
1555 if (net != LNET_NIDNET(LNET_NID_ANY) &&
1559 /* default rc to -1 in case we hit the goto */
1563 stats = (struct lnet_ioctl_element_stats *)ni_data->lic_bulk;
1564 lnd = (struct lnet_ioctl_config_lnd_tunables *)
1565 (ni_data->lic_bulk + sizeof(*stats));
1567 if (rc_net != prev_net) {
1574 if (!cYAML_create_string(net_node, "net type",
1575 libcfs_net2str(rc_net)))
1578 tmp = cYAML_create_seq(net_node, "local NI(s)");
1584 /* create the tree to be printed. */
1585 item = cYAML_create_seq_item(tmp);
1589 if (first_seq == NULL)
1592 if (cYAML_create_string(item, "nid",
1593 libcfs_nid2str(ni_data->lic_nid)) == NULL)
1596 if (cYAML_create_string(item,
1598 (ni_data->lic_status ==
1599 LNET_NI_STATUS_UP) ?
1600 "up" : "down") == NULL)
1603 /* don't add interfaces unless there is at least one
1605 if (strlen(ni_data->lic_ni_intf[0]) > 0) {
1606 interfaces = cYAML_create_object(item, "interfaces");
1607 if (interfaces == NULL)
1610 for (j = 0; j < LNET_MAX_INTERFACES; j++) {
1611 if (strlen(ni_data->lic_ni_intf[j]) > 0) {
1613 sizeof(str_buf), "%d", j);
1614 if (cYAML_create_string(interfaces,
1616 ni_data->lic_ni_intf[j]) ==
1626 statistics = cYAML_create_object(item, "statistics");
1627 if (statistics == NULL)
1630 if (cYAML_create_number(statistics, "send_count",
1635 if (cYAML_create_number(statistics, "recv_count",
1640 if (cYAML_create_number(statistics, "drop_count",
1645 tunables = cYAML_create_object(item, "tunables");
1649 rc = lustre_net_show_tunables(tunables, &lnd->lt_cmn);
1650 if (rc != LUSTRE_CFG_RC_NO_ERR)
1653 tunables = cYAML_create_object(item, "lnd tunables");
1654 if (tunables == NULL)
1657 rc = lustre_ni_show_tunables(tunables, LNET_NETTYP(rc_net),
1659 if (rc != LUSTRE_CFG_RC_NO_ERR)
1662 if (cYAML_create_number(item, "tcp bonding",
1663 ni_data->lic_tcp_bonding)
1667 if (cYAML_create_number(item, "dev cpt",
1668 ni_data->lic_dev_cpt) == NULL)
1671 /* out put the CPTs in the format: "[x,x,x,...]" */
1672 limit = str_buf + str_buf_len - 3;
1673 pos += snprintf(pos, limit - pos, "\"[");
1674 for (j = 0 ; ni_data->lic_ncpts >= 1 &&
1675 j < ni_data->lic_ncpts &&
1677 pos += snprintf(pos, limit - pos,
1678 "%d", ni_data->lic_cpts[j]);
1679 if ((j + 1) < ni_data->lic_ncpts)
1680 pos += snprintf(pos, limit - pos, ",");
1682 pos += snprintf(pos, 3, "]\"");
1684 if (ni_data->lic_ncpts >= 1 &&
1685 cYAML_create_string(item, "CPT",
1691 /* Print out the net information only if show_rc is not provided */
1692 if (show_rc == NULL)
1693 cYAML_print_tree(root);
1695 if (l_errno != ENOENT) {
1698 "\"cannot get networks: %s\"",
1703 rc = LUSTRE_CFG_RC_NO_ERR;
1705 snprintf(err_str, sizeof(err_str), "\"success\"");
1707 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
1708 cYAML_free_tree(root);
1709 } else if (show_rc != NULL && *show_rc != NULL) {
1710 struct cYAML *show_node;
1711 /* find the net node, if one doesn't exist
1712 * then insert one. Otherwise add to the one there
1714 show_node = cYAML_get_object_item(*show_rc, "net");
1715 if (show_node != NULL && cYAML_is_sequence(show_node)) {
1716 cYAML_insert_child(show_node, first_seq);
1719 } else if (show_node == NULL) {
1720 cYAML_insert_sibling((*show_rc)->cy_child,
1724 cYAML_free_tree(root);
1730 cYAML_build_error(rc, seq_no, SHOW_CMD, "net", err_str, err_rc);
1735 int lustre_lnet_enable_routing(int enable, int seq_no, struct cYAML **err_rc)
1737 struct lnet_ioctl_config_data data;
1738 int rc = LUSTRE_CFG_RC_NO_ERR;
1739 char err_str[LNET_MAX_STR_LEN];
1741 snprintf(err_str, sizeof(err_str), "\"success\"");
1743 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1744 data.cfg_config_u.cfg_buffers.buf_enable = (enable) ? 1 : 0;
1746 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIG_RTR, &data);
1751 "\"cannot %s routing %s\"",
1752 (enable) ? "enable" : "disable", strerror(errno));
1757 cYAML_build_error(rc, seq_no,
1758 (enable) ? ADD_CMD : DEL_CMD,
1759 "routing", err_str, err_rc);
1764 int lustre_lnet_config_numa_range(int range, int seq_no, struct cYAML **err_rc)
1766 struct lnet_ioctl_numa_range data;
1767 int rc = LUSTRE_CFG_RC_NO_ERR;
1768 char err_str[LNET_MAX_STR_LEN];
1770 snprintf(err_str, sizeof(err_str), "\"success\"");
1775 "\"range must be >= 0\"");
1776 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1780 LIBCFS_IOC_INIT_V2(data, nr_hdr);
1781 data.nr_range = range;
1783 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_SET_NUMA_RANGE, &data);
1788 "\"cannot configure buffers: %s\"", strerror(errno));
1793 cYAML_build_error(rc, seq_no, ADD_CMD, "numa_range", err_str, err_rc);
1798 int lustre_lnet_config_buffers(int tiny, int small, int large, int seq_no,
1799 struct cYAML **err_rc)
1801 struct lnet_ioctl_config_data data;
1802 int rc = LUSTRE_CFG_RC_NO_ERR;
1803 char err_str[LNET_MAX_STR_LEN];
1805 snprintf(err_str, sizeof(err_str), "\"success\"");
1807 /* -1 indicates to ignore changes to this field */
1808 if (tiny < -1 || small < -1 || large < -1) {
1811 "\"tiny, small and large must be >= 0\"");
1812 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1816 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1817 data.cfg_config_u.cfg_buffers.buf_tiny = tiny;
1818 data.cfg_config_u.cfg_buffers.buf_small = small;
1819 data.cfg_config_u.cfg_buffers.buf_large = large;
1821 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_BUF, &data);
1826 "\"cannot configure buffers: %s\"", strerror(errno));
1831 cYAML_build_error(rc, seq_no, ADD_CMD, "buf", err_str, err_rc);
1836 int lustre_lnet_show_routing(int seq_no, struct cYAML **show_rc,
1837 struct cYAML **err_rc)
1839 struct lnet_ioctl_config_data *data;
1840 struct lnet_ioctl_pool_cfg *pool_cfg = NULL;
1841 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1844 char *pools[LNET_NRBPOOLS] = {"tiny", "small", "large"};
1845 int buf_count[LNET_NRBPOOLS] = {0};
1846 struct cYAML *root = NULL, *pools_node = NULL,
1847 *type_node = NULL, *item = NULL, *cpt = NULL,
1848 *first_seq = NULL, *buffers = NULL;
1850 char err_str[LNET_MAX_STR_LEN];
1851 char node_name[LNET_MAX_STR_LEN];
1854 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1856 buf = calloc(1, sizeof(*data) + sizeof(*pool_cfg));
1860 data = (struct lnet_ioctl_config_data *)buf;
1862 root = cYAML_create_object(NULL, NULL);
1866 pools_node = cYAML_create_seq(root, "routing");
1867 if (pools_node == NULL)
1871 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
1872 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
1873 sizeof(struct lnet_ioctl_pool_cfg);
1874 data->cfg_count = i;
1876 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_BUF, data);
1884 pool_cfg = (struct lnet_ioctl_pool_cfg *)data->cfg_bulk;
1886 snprintf(node_name, sizeof(node_name), "cpt[%d]", i);
1887 item = cYAML_create_seq_item(pools_node);
1891 if (first_seq == NULL)
1894 cpt = cYAML_create_object(item, node_name);
1898 /* create the tree and print */
1899 for (j = 0; j < LNET_NRBPOOLS; j++) {
1900 type_node = cYAML_create_object(cpt, pools[j]);
1901 if (type_node == NULL)
1903 if (cYAML_create_number(type_node, "npages",
1904 pool_cfg->pl_pools[j].pl_npages)
1907 if (cYAML_create_number(type_node, "nbuffers",
1908 pool_cfg->pl_pools[j].
1909 pl_nbuffers) == NULL)
1911 if (cYAML_create_number(type_node, "credits",
1912 pool_cfg->pl_pools[j].
1913 pl_credits) == NULL)
1915 if (cYAML_create_number(type_node, "mincredits",
1916 pool_cfg->pl_pools[j].
1917 pl_mincredits) == NULL)
1919 /* keep track of the total count for each of the
1920 * tiny, small and large buffers */
1921 buf_count[j] += pool_cfg->pl_pools[j].pl_nbuffers;
1925 if (pool_cfg != NULL) {
1926 item = cYAML_create_seq_item(pools_node);
1930 if (cYAML_create_number(item, "enable", pool_cfg->pl_routing) ==
1935 /* create a buffers entry in the show. This is necessary so that
1936 * if the YAML output is used to configure a node, the buffer
1937 * configuration takes hold */
1938 buffers = cYAML_create_object(root, "buffers");
1939 if (buffers == NULL)
1942 for (i = 0; i < LNET_NRBPOOLS; i++) {
1943 if (cYAML_create_number(buffers, pools[i], buf_count[i]) == NULL)
1947 if (show_rc == NULL)
1948 cYAML_print_tree(root);
1950 if (l_errno != ENOENT) {
1953 "\"cannot get routing information: %s\"",
1958 rc = LUSTRE_CFG_RC_NO_ERR;
1960 snprintf(err_str, sizeof(err_str), "\"success\"");
1961 rc = LUSTRE_CFG_RC_NO_ERR;
1965 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
1966 cYAML_free_tree(root);
1967 } else if (show_rc != NULL && *show_rc != NULL) {
1968 struct cYAML *routing_node;
1969 /* there should exist only one routing block and one
1970 * buffers block. If there already exists a previous one
1971 * then don't add another */
1972 routing_node = cYAML_get_object_item(*show_rc, "routing");
1973 if (routing_node == NULL) {
1974 cYAML_insert_sibling((*show_rc)->cy_child,
1978 cYAML_free_tree(root);
1984 cYAML_build_error(rc, seq_no, SHOW_CMD, "routing", err_str, err_rc);
1989 int lustre_lnet_show_peer(char *knid, int detail, int seq_no,
1990 struct cYAML **show_rc, struct cYAML **err_rc)
1993 * TODO: This function is changing in a future patch to accommodate
1994 * PEER_LIST and proper filtering on any nid of the peer
1996 struct lnet_ioctl_peer_cfg *peer_info;
1997 struct lnet_peer_ni_credit_info *lpni_cri;
1998 struct lnet_ioctl_element_stats *lpni_stats;
1999 int rc = LUSTRE_CFG_RC_OUT_OF_MEM, ncpt = 0, i = 0, j = 0;
2001 struct cYAML *root = NULL, *peer = NULL, *peer_ni = NULL,
2002 *first_seq = NULL, *peer_root = NULL, *tmp = NULL;
2003 char err_str[LNET_MAX_STR_LEN];
2004 lnet_nid_t prev_primary_nid = LNET_NID_ANY, primary_nid = LNET_NID_ANY;
2005 int data_size = sizeof(*peer_info) + sizeof(*lpni_cri) +
2006 sizeof(*lpni_stats);
2007 char *data = calloc(data_size, 1);
2008 bool new_peer = true;
2010 snprintf(err_str, sizeof(err_str),
2011 "\"out of memory\"");
2016 peer_info = (struct lnet_ioctl_peer_cfg *)data;
2018 /* create struct cYAML root object */
2019 root = cYAML_create_object(NULL, NULL);
2023 peer_root = cYAML_create_seq(root, "peer");
2024 if (peer_root == NULL)
2028 primary_nid = libcfs_str2nid(knid);
2032 memset(data, 0, data_size);
2033 LIBCFS_IOC_INIT_V2(*peer_info, prcfg_hdr);
2034 peer_info->prcfg_hdr.ioc_len = data_size;
2035 peer_info->prcfg_idx = i;
2037 rc = l_ioctl(LNET_DEV_ID,
2038 IOC_LIBCFS_GET_PEER_NI, peer_info);
2044 if (primary_nid != LNET_NID_ANY &&
2045 primary_nid != peer_info->prcfg_prim_nid)
2048 lpni_cri = (struct lnet_peer_ni_credit_info*)peer_info->prcfg_bulk;
2049 lpni_stats = (struct lnet_ioctl_element_stats *)
2050 (peer_info->prcfg_bulk +
2053 peer = cYAML_create_seq_item(peer_root);
2057 if (peer_info->prcfg_prim_nid != prev_primary_nid) {
2058 prev_primary_nid = peer_info->prcfg_prim_nid;
2063 lnet_nid_t pnid = peer_info->prcfg_prim_nid;
2064 if (cYAML_create_string(peer, "primary nid",
2065 libcfs_nid2str(pnid))
2068 if (cYAML_create_string(peer, "Multi-Rail",
2069 peer_info->prcfg_mr ?
2073 tmp = cYAML_create_seq(peer, "peer ni");
2079 if (first_seq == NULL)
2082 peer_ni = cYAML_create_seq_item(tmp);
2083 if (peer_ni == NULL)
2086 if (cYAML_create_string(peer_ni, "nid",
2088 (peer_info->prcfg_cfg_nid))
2092 if (cYAML_create_string(peer_ni, "state",
2093 lpni_cri->cr_aliveness)
2100 if (cYAML_create_number(peer_ni, "max_ni_tx_credits",
2101 lpni_cri->cr_ni_peer_tx_credits)
2105 if (cYAML_create_number(peer_ni, "available_tx_credits",
2106 lpni_cri->cr_peer_tx_credits)
2110 if (cYAML_create_number(peer_ni, "min_tx_credits",
2111 lpni_cri->cr_peer_min_tx_credits)
2115 if (cYAML_create_number(peer_ni, "tx_q_num_of_buf",
2116 lpni_cri->cr_peer_tx_qnob)
2120 if (cYAML_create_number(peer_ni, "available_rtr_credits",
2121 lpni_cri->cr_peer_rtr_credits)
2125 if (cYAML_create_number(peer_ni, "min_rtr_credits",
2126 lpni_cri->cr_peer_min_rtr_credits)
2130 if (cYAML_create_number(peer_ni, "send_count",
2131 lpni_stats->send_count)
2135 if (cYAML_create_number(peer_ni, "recv_count",
2136 lpni_stats->recv_count)
2140 if (cYAML_create_number(peer_ni, "drop_count",
2141 lpni_stats->drop_count)
2145 if (cYAML_create_number(peer_ni, "refcount",
2146 lpni_cri->cr_refcount) == NULL)
2150 if (l_errno != ENOENT) {
2153 "\"cannot get peer information: %s\"",
2162 /* print output iff show_rc is not provided */
2163 if (show_rc == NULL)
2164 cYAML_print_tree(root);
2166 snprintf(err_str, sizeof(err_str), "\"success\"");
2167 rc = LUSTRE_CFG_RC_NO_ERR;
2170 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2171 cYAML_free_tree(root);
2172 } else if (show_rc != NULL && *show_rc != NULL) {
2173 struct cYAML *show_node;
2174 /* find the peer node, if one doesn't exist then
2175 * insert one. Otherwise add to the one there
2177 show_node = cYAML_get_object_item(*show_rc,
2179 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2180 cYAML_insert_child(show_node, first_seq);
2183 } else if (show_node == NULL) {
2184 cYAML_insert_sibling((*show_rc)->cy_child,
2188 cYAML_free_tree(root);
2194 cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
2200 int lustre_lnet_show_numa_range(int seq_no, struct cYAML **show_rc,
2201 struct cYAML **err_rc)
2203 struct lnet_ioctl_numa_range data;
2204 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2206 char err_str[LNET_MAX_STR_LEN];
2207 struct cYAML *root = NULL, *range = NULL;
2209 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2211 LIBCFS_IOC_INIT_V2(data, nr_hdr);
2213 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NUMA_RANGE, &data);
2218 "\"cannot get numa range: %s\"",
2224 root = cYAML_create_object(NULL, NULL);
2228 range = cYAML_create_object(root, "numa");
2232 if (cYAML_create_number(range, "range",
2233 data.nr_range) == NULL)
2236 if (show_rc == NULL)
2237 cYAML_print_tree(root);
2239 snprintf(err_str, sizeof(err_str), "\"success\"");
2241 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2242 cYAML_free_tree(root);
2243 } else if (show_rc != NULL && *show_rc != NULL) {
2244 cYAML_insert_sibling((*show_rc)->cy_child,
2251 cYAML_build_error(rc, seq_no, SHOW_CMD, "numa", err_str, err_rc);
2256 int lustre_lnet_show_stats(int seq_no, struct cYAML **show_rc,
2257 struct cYAML **err_rc)
2259 struct lnet_ioctl_lnet_stats data;
2260 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2262 char err_str[LNET_MAX_STR_LEN];
2263 struct cYAML *root = NULL, *stats = NULL;
2265 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2267 LIBCFS_IOC_INIT_V2(data, st_hdr);
2269 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LNET_STATS, &data);
2274 "\"cannot get lnet statistics: %s\"",
2280 root = cYAML_create_object(NULL, NULL);
2284 stats = cYAML_create_object(root, "statistics");
2288 if (cYAML_create_number(stats, "msgs_alloc",
2289 data.st_cntrs.msgs_alloc) == NULL)
2292 if (cYAML_create_number(stats, "msgs_max",
2293 data.st_cntrs.msgs_max) == NULL)
2296 if (cYAML_create_number(stats, "errors",
2297 data.st_cntrs.errors) == NULL)
2300 if (cYAML_create_number(stats, "send_count",
2301 data.st_cntrs.send_count) == NULL)
2304 if (cYAML_create_number(stats, "recv_count",
2305 data.st_cntrs.recv_count) == NULL)
2308 if (cYAML_create_number(stats, "route_count",
2309 data.st_cntrs.route_count) == NULL)
2312 if (cYAML_create_number(stats, "drop_count",
2313 data.st_cntrs.drop_count) == NULL)
2316 if (cYAML_create_number(stats, "send_length",
2317 data.st_cntrs.send_length) == NULL)
2320 if (cYAML_create_number(stats, "recv_length",
2321 data.st_cntrs.recv_length) == NULL)
2324 if (cYAML_create_number(stats, "route_length",
2325 data.st_cntrs.route_length) == NULL)
2328 if (cYAML_create_number(stats, "drop_length",
2329 data.st_cntrs.drop_length) == NULL)
2332 if (show_rc == NULL)
2333 cYAML_print_tree(root);
2335 snprintf(err_str, sizeof(err_str), "\"success\"");
2337 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2338 cYAML_free_tree(root);
2339 } else if (show_rc != NULL && *show_rc != NULL) {
2340 cYAML_insert_sibling((*show_rc)->cy_child,
2347 cYAML_build_error(rc, seq_no, SHOW_CMD, "statistics", err_str, err_rc);
2352 typedef int (*cmd_handler_t)(struct cYAML *tree,
2353 struct cYAML **show_rc,
2354 struct cYAML **err_rc);
2356 static int handle_yaml_config_route(struct cYAML *tree, struct cYAML **show_rc,
2357 struct cYAML **err_rc)
2359 struct cYAML *net, *gw, *hop, *prio, *seq_no;
2361 net = cYAML_get_object_item(tree, "net");
2362 gw = cYAML_get_object_item(tree, "gateway");
2363 hop = cYAML_get_object_item(tree, "hop");
2364 prio = cYAML_get_object_item(tree, "priority");
2365 seq_no = cYAML_get_object_item(tree, "seq_no");
2367 return lustre_lnet_config_route((net) ? net->cy_valuestring : NULL,
2368 (gw) ? gw->cy_valuestring : NULL,
2369 (hop) ? hop->cy_valueint : -1,
2370 (prio) ? prio->cy_valueint : -1,
2371 (seq_no) ? seq_no->cy_valueint : -1,
2375 static void yaml_free_string_array(char **array, int num)
2378 char **sub_array = array;
2380 for (i = 0; i < num; i++) {
2381 if (*sub_array != NULL)
2391 * 0: <intf_name>['['<expr>']']
2392 * 1: <intf_name>['['<expr>']']
2394 static int yaml_copy_intf_info(struct cYAML *intf_tree,
2395 struct lnet_dlc_network_descr *nw_descr)
2397 struct cYAML *child = NULL;
2398 int intf_num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
2399 struct lnet_dlc_intf_descr *intf_descr, *tmp;
2401 if (intf_tree == NULL || nw_descr == NULL)
2402 return LUSTRE_CFG_RC_BAD_PARAM;
2404 /* now grab all the interfaces and their cpts */
2405 child = intf_tree->cy_child;
2406 while (child != NULL) {
2407 if (child->cy_valuestring == NULL) {
2408 child = child->cy_next;
2412 if (strlen(child->cy_valuestring) >= LNET_MAX_STR_LEN)
2415 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist,
2416 child->cy_valuestring,
2417 strlen(child->cy_valuestring));
2418 if (rc != LUSTRE_CFG_RC_NO_ERR)
2422 child = child->cy_next;
2426 return LUSTRE_CFG_RC_MISSING_PARAM;
2431 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
2433 list_del(&intf_descr->intf_on_network);
2434 free_intf_descr(intf_descr);
2441 yaml_extract_cmn_tunables(struct cYAML *tree,
2442 struct lnet_ioctl_config_lnd_cmn_tunables *tunables,
2443 struct cfs_expr_list **global_cpts)
2445 struct cYAML *tun, *item, *smp;
2448 tun = cYAML_get_object_item(tree, "tunables");
2450 item = cYAML_get_object_item(tun, "peer_timeout");
2452 tunables->lct_peer_timeout = item->cy_valueint;
2453 item = cYAML_get_object_item(tun, "peer_credits");
2455 tunables->lct_peer_tx_credits = item->cy_valueint;
2456 item = cYAML_get_object_item(tun, "peer_buffer_credits");
2458 tunables->lct_peer_rtr_credits = item->cy_valueint;
2459 item = cYAML_get_object_item(tun, "credits");
2461 tunables->lct_max_tx_credits = item->cy_valueint;
2462 smp = cYAML_get_object_item(tun, "CPT");
2464 rc = cfs_expr_list_parse(smp->cy_valuestring,
2465 strlen(smp->cy_valuestring),
2466 0, UINT_MAX, global_cpts);
2468 *global_cpts = NULL;
2478 yaml_extract_tunables(struct cYAML *tree,
2479 struct lnet_ioctl_config_lnd_tunables *tunables,
2480 struct cfs_expr_list **global_cpts,
2485 rc = yaml_extract_cmn_tunables(tree, &tunables->lt_cmn,
2491 lustre_yaml_extract_lnd_tunables(tree, net_type,
2499 * - net type: <net>[<NUM>]
2501 * - nid: <ip>@<net>[<NUM>]
2504 * 0: <intf_name>['['<expr>']']
2505 * 1: <intf_name>['['<expr>']']
2507 * peer_timeout: <NUM>
2508 * peer_credits: <NUM>
2509 * peer_buffer_credits: <NUM>
2512 * peercredits_hiw: <NUM>
2513 * map_on_demand: <NUM>
2514 * concurrent_sends: <NUM>
2515 * fmr_pool_size: <NUM>
2516 * fmr_flush_trigger: <NUM>
2519 * At least one interface is required. If no interfaces are provided the
2520 * network interface can not be configured.
2522 static int handle_yaml_config_ni(struct cYAML *tree, struct cYAML **show_rc,
2523 struct cYAML **err_rc)
2525 struct cYAML *net, *intf, *seq_no, *ip2net = NULL, *local_nis = NULL,
2527 int num_entries = 0, rc;
2528 struct lnet_dlc_network_descr nw_descr;
2529 struct cfs_expr_list *global_cpts = NULL;
2530 struct lnet_ioctl_config_lnd_tunables tunables;
2533 memset(&tunables, 0, sizeof(tunables));
2535 INIT_LIST_HEAD(&nw_descr.network_on_rule);
2536 INIT_LIST_HEAD(&nw_descr.nw_intflist);
2538 ip2net = cYAML_get_object_item(tree, "ip2net");
2539 net = cYAML_get_object_item(tree, "net type");
2541 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
2544 * if neither net nor ip2nets are present, then we can not
2545 * configure the network.
2547 if (!net && !ip2net)
2548 return LUSTRE_CFG_RC_MISSING_PARAM;
2550 local_nis = cYAML_get_object_item(tree, "local NI(s)");
2551 if (local_nis == NULL)
2552 return LUSTRE_CFG_RC_MISSING_PARAM;
2554 if (!cYAML_is_sequence(local_nis))
2555 return LUSTRE_CFG_RC_BAD_PARAM;
2557 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
2558 intf = cYAML_get_object_item(item, "interfaces");
2561 num_entries = yaml_copy_intf_info(intf, &nw_descr);
2562 if (num_entries <= 0) {
2563 cYAML_build_error(num_entries, -1, "ni", "add",
2564 "bad interface list",
2566 return LUSTRE_CFG_RC_BAD_PARAM;
2570 found = yaml_extract_tunables(tree, &tunables, &global_cpts,
2571 LNET_NETTYP(nw_descr.nw_id));
2572 seq_no = cYAML_get_object_item(tree, "seq_no");
2574 rc = lustre_lnet_config_ni(&nw_descr,
2576 (ip2net) ? ip2net->cy_valuestring : NULL,
2577 (found) ? &tunables: NULL,
2578 (seq_no) ? seq_no->cy_valueint : -1,
2581 if (global_cpts != NULL)
2582 cfs_expr_list_free(global_cpts);
2589 * - net-spec: <tcp|o2ib|gni>[NUM]
2591 * 0: <intf name>['['<expr>']']
2592 * 1: <intf name>['['<expr>']']
2594 * 0: <expr.expr.expr.expr>
2595 * 1: <expr.expr.expr.expr>
2597 static int handle_yaml_config_ip2nets(struct cYAML *tree,
2598 struct cYAML **show_rc,
2599 struct cYAML **err_rc)
2601 struct cYAML *net, *ip_range, *item = NULL, *intf = NULL,
2603 struct lustre_lnet_ip2nets ip2nets;
2604 struct lustre_lnet_ip_range_descr *ip_range_descr = NULL,
2606 int rc = LUSTRE_CFG_RC_NO_ERR;
2607 struct cfs_expr_list *global_cpts = NULL;
2608 struct cfs_expr_list *el, *el_tmp;
2609 struct lnet_ioctl_config_lnd_tunables tunables;
2610 struct lnet_dlc_intf_descr *intf_descr, *intf_tmp;
2613 memset(&tunables, 0, sizeof(tunables));
2615 /* initialize all lists */
2616 INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
2617 INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
2618 INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
2620 net = cYAML_get_object_item(tree, "net-spec");
2622 return LUSTRE_CFG_RC_BAD_PARAM;
2624 if (net != NULL && net->cy_valuestring == NULL)
2625 return LUSTRE_CFG_RC_BAD_PARAM;
2627 /* assign the network id */
2628 ip2nets.ip2nets_net.nw_id = libcfs_str2net(net->cy_valuestring);
2629 if (ip2nets.ip2nets_net.nw_id == LNET_NID_ANY)
2630 return LUSTRE_CFG_RC_BAD_PARAM;
2632 seq_no = cYAML_get_object_item(tree, "seq_no");
2634 intf = cYAML_get_object_item(tree, "interfaces");
2636 rc = yaml_copy_intf_info(intf, &ip2nets.ip2nets_net);
2638 return LUSTRE_CFG_RC_BAD_PARAM;
2641 ip_range = cYAML_get_object_item(tree, "ip-range");
2642 if (ip_range != NULL) {
2643 item = ip_range->cy_child;
2644 while (item != NULL) {
2645 if (item->cy_valuestring == NULL) {
2646 item = item->cy_next;
2650 rc = lustre_lnet_add_ip_range(&ip2nets.ip2nets_ip_ranges,
2651 item->cy_valuestring);
2653 if (rc != LUSTRE_CFG_RC_NO_ERR)
2656 item = item->cy_next;
2660 found = yaml_extract_tunables(tree, &tunables, &global_cpts,
2661 LNET_NETTYP(ip2nets.ip2nets_net.nw_id));
2663 rc = lustre_lnet_config_ip2nets(&ip2nets,
2664 (found) ? &tunables : NULL,
2666 (seq_no) ? seq_no->cy_valueint : -1,
2670 * don't stop because there was no match. Continue processing the
2671 * rest of the rules. If non-match then nothing is configured
2673 if (rc == LUSTRE_CFG_RC_NO_MATCH)
2674 rc = LUSTRE_CFG_RC_NO_ERR;
2676 list_for_each_entry_safe(intf_descr, intf_tmp,
2677 &ip2nets.ip2nets_net.nw_intflist,
2679 list_del(&intf_descr->intf_on_network);
2680 free_intf_descr(intf_descr);
2683 list_for_each_entry_safe(ip_range_descr, tmp,
2684 &ip2nets.ip2nets_ip_ranges,
2686 list_del(&ip_range_descr->ipr_entry);
2687 list_for_each_entry_safe(el, el_tmp, &ip_range_descr->ipr_expr,
2689 list_del(&el->el_link);
2690 cfs_expr_list_free(el);
2692 free(ip_range_descr);
2698 static int handle_yaml_del_ni(struct cYAML *tree, struct cYAML **show_rc,
2699 struct cYAML **err_rc)
2701 struct cYAML *net = NULL, *intf = NULL, *seq_no = NULL, *item = NULL,
2703 int num_entries, rc;
2704 struct lnet_dlc_network_descr nw_descr;
2706 INIT_LIST_HEAD(&nw_descr.network_on_rule);
2707 INIT_LIST_HEAD(&nw_descr.nw_intflist);
2709 net = cYAML_get_object_item(tree, "net type");
2711 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
2713 local_nis = cYAML_get_object_item(tree, "local NI(s)");
2714 if (local_nis == NULL)
2715 return LUSTRE_CFG_RC_MISSING_PARAM;
2717 if (!cYAML_is_sequence(local_nis))
2718 return LUSTRE_CFG_RC_BAD_PARAM;
2720 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
2721 intf = cYAML_get_object_item(item, "interfaces");
2724 num_entries = yaml_copy_intf_info(intf, &nw_descr);
2725 if (num_entries <= 0) {
2726 cYAML_build_error(num_entries, -1, "ni", "add",
2727 "bad interface list",
2729 return LUSTRE_CFG_RC_BAD_PARAM;
2733 seq_no = cYAML_get_object_item(tree, "seq_no");
2735 rc = lustre_lnet_del_ni((net) ? &nw_descr : NULL,
2736 (seq_no) ? seq_no->cy_valueint : -1,
2742 static int yaml_copy_peer_nids(struct cYAML *tree, char ***nidsppp)
2744 struct cYAML *nids_entry = NULL, *child = NULL, *entry = NULL;
2746 int num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
2748 nids_entry = cYAML_get_object_item(tree, "peer ni");
2749 if (cYAML_is_sequence(nids_entry)) {
2750 while (cYAML_get_next_seq_item(nids_entry, &child))
2755 return LUSTRE_CFG_RC_MISSING_PARAM;
2757 nids = calloc(sizeof(*nids) * num, 1);
2759 return LUSTRE_CFG_RC_OUT_OF_MEM;
2761 /* now grab all the nids */
2764 while (cYAML_get_next_seq_item(nids_entry, &child)) {
2765 entry = cYAML_get_object_item(child, "nid");
2768 nids[num] = calloc(strlen(entry->cy_valuestring) + 1, 1);
2770 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2773 strncpy(nids[num], entry->cy_valuestring,
2774 strlen(entry->cy_valuestring));
2784 yaml_free_string_array(nids, num);
2789 static int handle_yaml_config_peer(struct cYAML *tree, struct cYAML **show_rc,
2790 struct cYAML **err_rc)
2794 struct cYAML *seq_no, *prim_nid, *non_mr;
2796 num = yaml_copy_peer_nids(tree, &nids);
2800 seq_no = cYAML_get_object_item(tree, "seq_no");
2801 prim_nid = cYAML_get_object_item(tree, "primary nid");
2802 non_mr = cYAML_get_object_item(tree, "non_mr");
2804 rc = lustre_lnet_config_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
2806 (non_mr) ? false : true,
2807 (seq_no) ? seq_no->cy_valueint : -1,
2810 yaml_free_string_array(nids, num);
2814 static int handle_yaml_del_peer(struct cYAML *tree, struct cYAML **show_rc,
2815 struct cYAML **err_rc)
2819 struct cYAML *seq_no, *prim_nid;
2821 num = yaml_copy_peer_nids(tree, &nids);
2825 seq_no = cYAML_get_object_item(tree, "seq_no");
2826 prim_nid = cYAML_get_object_item(tree, "primary nid");
2828 rc = lustre_lnet_del_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
2830 (seq_no) ? seq_no->cy_valueint : -1,
2833 yaml_free_string_array(nids, num);
2837 static int handle_yaml_config_buffers(struct cYAML *tree,
2838 struct cYAML **show_rc,
2839 struct cYAML **err_rc)
2842 struct cYAML *tiny, *small, *large, *seq_no;
2844 tiny = cYAML_get_object_item(tree, "tiny");
2845 small = cYAML_get_object_item(tree, "small");
2846 large = cYAML_get_object_item(tree, "large");
2847 seq_no = cYAML_get_object_item(tree, "seq_no");
2849 rc = lustre_lnet_config_buffers((tiny) ? tiny->cy_valueint : -1,
2850 (small) ? small->cy_valueint : -1,
2851 (large) ? large->cy_valueint : -1,
2852 (seq_no) ? seq_no->cy_valueint : -1,
2858 static int handle_yaml_config_routing(struct cYAML *tree,
2859 struct cYAML **show_rc,
2860 struct cYAML **err_rc)
2862 int rc = LUSTRE_CFG_RC_NO_ERR;
2863 struct cYAML *seq_no, *enable;
2865 seq_no = cYAML_get_object_item(tree, "seq_no");
2866 enable = cYAML_get_object_item(tree, "enable");
2869 rc = lustre_lnet_enable_routing(enable->cy_valueint,
2871 seq_no->cy_valueint : -1,
2878 static int handle_yaml_del_route(struct cYAML *tree, struct cYAML **show_rc,
2879 struct cYAML **err_rc)
2883 struct cYAML *seq_no;
2885 net = cYAML_get_object_item(tree, "net");
2886 gw = cYAML_get_object_item(tree, "gateway");
2887 seq_no = cYAML_get_object_item(tree, "seq_no");
2889 return lustre_lnet_del_route((net) ? net->cy_valuestring : NULL,
2890 (gw) ? gw->cy_valuestring : NULL,
2891 (seq_no) ? seq_no->cy_valueint : -1,
2895 static int handle_yaml_del_routing(struct cYAML *tree, struct cYAML **show_rc,
2896 struct cYAML **err_rc)
2898 struct cYAML *seq_no;
2900 seq_no = cYAML_get_object_item(tree, "seq_no");
2902 return lustre_lnet_enable_routing(0, (seq_no) ?
2903 seq_no->cy_valueint : -1,
2907 static int handle_yaml_show_route(struct cYAML *tree, struct cYAML **show_rc,
2908 struct cYAML **err_rc)
2914 struct cYAML *detail;
2915 struct cYAML *seq_no;
2917 net = cYAML_get_object_item(tree, "net");
2918 gw = cYAML_get_object_item(tree, "gateway");
2919 hop = cYAML_get_object_item(tree, "hop");
2920 prio = cYAML_get_object_item(tree, "priority");
2921 detail = cYAML_get_object_item(tree, "detail");
2922 seq_no = cYAML_get_object_item(tree, "seq_no");
2924 return lustre_lnet_show_route((net) ? net->cy_valuestring : NULL,
2925 (gw) ? gw->cy_valuestring : NULL,
2926 (hop) ? hop->cy_valueint : -1,
2927 (prio) ? prio->cy_valueint : -1,
2928 (detail) ? detail->cy_valueint : 0,
2929 (seq_no) ? seq_no->cy_valueint : -1,
2934 static int handle_yaml_show_net(struct cYAML *tree, struct cYAML **show_rc,
2935 struct cYAML **err_rc)
2937 struct cYAML *net, *detail, *seq_no;
2939 net = cYAML_get_object_item(tree, "net");
2940 detail = cYAML_get_object_item(tree, "detail");
2941 seq_no = cYAML_get_object_item(tree, "seq_no");
2943 return lustre_lnet_show_net((net) ? net->cy_valuestring : NULL,
2944 (detail) ? detail->cy_valueint : 0,
2945 (seq_no) ? seq_no->cy_valueint : -1,
2950 static int handle_yaml_show_routing(struct cYAML *tree, struct cYAML **show_rc,
2951 struct cYAML **err_rc)
2953 struct cYAML *seq_no;
2955 seq_no = cYAML_get_object_item(tree, "seq_no");
2957 return lustre_lnet_show_routing((seq_no) ? seq_no->cy_valueint : -1,
2961 static int handle_yaml_show_peers(struct cYAML *tree, struct cYAML **show_rc,
2962 struct cYAML **err_rc)
2964 struct cYAML *seq_no, *nid, *detail;
2966 seq_no = cYAML_get_object_item(tree, "seq_no");
2967 detail = cYAML_get_object_item(tree, "detail");
2968 nid = cYAML_get_object_item(tree, "nid");
2970 return lustre_lnet_show_peer((nid) ? nid->cy_valuestring : NULL,
2971 (detail) ? detail->cy_valueint : 0,
2972 (seq_no) ? seq_no->cy_valueint : -1,
2976 static int handle_yaml_show_stats(struct cYAML *tree, struct cYAML **show_rc,
2977 struct cYAML **err_rc)
2979 struct cYAML *seq_no;
2981 seq_no = cYAML_get_object_item(tree, "seq_no");
2983 return lustre_lnet_show_stats((seq_no) ? seq_no->cy_valueint : -1,
2987 static int handle_yaml_config_numa(struct cYAML *tree, struct cYAML **show_rc,
2988 struct cYAML **err_rc)
2990 struct cYAML *seq_no, *range;
2992 seq_no = cYAML_get_object_item(tree, "seq_no");
2993 range = cYAML_get_object_item(tree, "range");
2995 return lustre_lnet_config_numa_range(range ? range->cy_valueint : -1,
2996 seq_no ? seq_no->cy_valueint : -1,
3000 static int handle_yaml_del_numa(struct cYAML *tree, struct cYAML **show_rc,
3001 struct cYAML **err_rc)
3003 struct cYAML *seq_no;
3005 seq_no = cYAML_get_object_item(tree, "seq_no");
3007 return lustre_lnet_config_numa_range(0, seq_no ? seq_no->cy_valueint : -1,
3011 static int handle_yaml_show_numa(struct cYAML *tree, struct cYAML **show_rc,
3012 struct cYAML **err_rc)
3014 struct cYAML *seq_no;
3016 seq_no = cYAML_get_object_item(tree, "seq_no");
3018 return lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint : -1,
3022 struct lookup_cmd_hdlr_tbl {
3027 static struct lookup_cmd_hdlr_tbl lookup_config_tbl[] = {
3028 { .name = "route", .cb = handle_yaml_config_route },
3029 { .name = "net", .cb = handle_yaml_config_ni },
3030 { .name = "ip2nets", .cb = handle_yaml_config_ip2nets },
3031 { .name = "peer", .cb = handle_yaml_config_peer },
3032 { .name = "routing", .cb = handle_yaml_config_routing },
3033 { .name = "buffers", .cb = handle_yaml_config_buffers },
3034 { .name = "numa", .cb = handle_yaml_config_numa },
3037 static struct lookup_cmd_hdlr_tbl lookup_del_tbl[] = {
3038 { .name = "route", .cb = handle_yaml_del_route },
3039 { .name = "net", .cb = handle_yaml_del_ni },
3040 { .name = "peer", .cb = handle_yaml_del_peer },
3041 { .name = "routing", .cb = handle_yaml_del_routing },
3042 { .name = "numa", .cb = handle_yaml_del_numa },
3045 static struct lookup_cmd_hdlr_tbl lookup_show_tbl[] = {
3046 { .name = "route", .cb = handle_yaml_show_route },
3047 { .name = "net", .cb = handle_yaml_show_net },
3048 { .name = "buffers", .cb = handle_yaml_show_routing },
3049 { .name = "routing", .cb = handle_yaml_show_routing },
3050 { .name = "peer", .cb = handle_yaml_show_peers },
3051 { .name = "statistics", .cb = handle_yaml_show_stats },
3052 { .name = "numa", .cb = handle_yaml_show_numa },
3055 static cmd_handler_t lookup_fn(char *key,
3056 struct lookup_cmd_hdlr_tbl *tbl)
3062 for (i = 0; tbl[i].name != NULL; i++) {
3063 if (strncmp(key, tbl[i].name, strlen(tbl[i].name)) == 0)
3070 static int lustre_yaml_cb_helper(char *f, struct lookup_cmd_hdlr_tbl *table,
3071 struct cYAML **show_rc, struct cYAML **err_rc)
3073 struct cYAML *tree, *item = NULL, *head, *child;
3075 char err_str[LNET_MAX_STR_LEN];
3076 int rc = LUSTRE_CFG_RC_NO_ERR, return_rc = LUSTRE_CFG_RC_NO_ERR;
3078 tree = cYAML_build_tree(f, NULL, 0, err_rc, false);
3080 return LUSTRE_CFG_RC_BAD_PARAM;
3082 child = tree->cy_child;
3083 while (child != NULL) {
3084 cb = lookup_fn(child->cy_string, table);
3086 snprintf(err_str, sizeof(err_str),
3087 "\"call back for '%s' not found\"",
3089 cYAML_build_error(LUSTRE_CFG_RC_BAD_PARAM, -1,
3090 "yaml", "helper", err_str, err_rc);
3094 if (cYAML_is_sequence(child)) {
3095 while ((head = cYAML_get_next_seq_item(child, &item))
3097 rc = cb(head, show_rc, err_rc);
3098 if (rc != LUSTRE_CFG_RC_NO_ERR)
3102 rc = cb(child, show_rc, err_rc);
3103 if (rc != LUSTRE_CFG_RC_NO_ERR)
3107 child = child->cy_next;
3111 cYAML_free_tree(tree);
3116 int lustre_yaml_config(char *f, struct cYAML **err_rc)
3118 return lustre_yaml_cb_helper(f, lookup_config_tbl,
3122 int lustre_yaml_del(char *f, struct cYAML **err_rc)
3124 return lustre_yaml_cb_helper(f, lookup_del_tbl,
3128 int lustre_yaml_show(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
3130 return lustre_yaml_cb_helper(f, lookup_show_tbl,
3134 int lustre_lnet_send_dbg_task(enum lnet_dbg_task dbg_task,
3135 struct lnet_dbg_task_info *dbg_info,
3136 struct cYAML **show_rc,
3137 struct cYAML **err_rc)
3139 struct lnet_ioctl_dbg *dbg;
3140 struct lnet_dbg_task_info *info;
3141 int rc = LUSTRE_CFG_RC_NO_ERR;
3142 char err_str[LNET_MAX_STR_LEN];
3144 snprintf(err_str, sizeof(err_str), "\"success\"");
3146 dbg = calloc(1, sizeof(*dbg) + sizeof(*info));
3148 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3149 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3153 info = (struct lnet_dbg_task_info *)dbg->dbg_bulk;
3155 LIBCFS_IOC_INIT_V2(*dbg, dbg_hdr);
3157 dbg->dbg_task = dbg_task;
3159 memcpy(info, dbg_info, sizeof(*info));
3161 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DBG, dbg);
3166 "\"debug task failed %s\"", strerror(errno));
3171 cYAML_build_error(rc, -1, DBG_CMD,
3172 "debug", err_str, err_rc);