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 if (LNET_NETTYP(net) == CIBLND ||
588 LNET_NETTYP(net) == OPENIBLND ||
589 LNET_NETTYP(net) == IIBLND ||
590 LNET_NETTYP(net) == VIBLND) {
593 "\"obselete LNet type '%s'\"", libcfs_lnd2str(net));
594 rc = LUSTRE_CFG_RC_BAD_PARAM;
598 gateway_nid = libcfs_str2nid(gw);
599 if (gateway_nid == LNET_NID_ANY) {
602 "\"cannot parse gateway NID '%s'\"", gw);
603 rc = LUSTRE_CFG_RC_BAD_PARAM;
608 /* hops is undefined */
609 hops = LNET_UNDEFINED_HOPS;
610 } else if (hops < 1 || hops > 255) {
613 "\"invalid hop count %d, must be between 1 and 255\"",
615 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
621 } else if (prio < 0) {
624 "\"invalid priority %d, must be greater than 0\"",
626 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
630 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
632 data.cfg_config_u.cfg_route.rtr_hop = hops;
633 data.cfg_config_u.cfg_route.rtr_priority = prio;
634 data.cfg_nid = gateway_nid;
636 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
641 "\"cannot add route: %s\"", strerror(errno));
646 cYAML_build_error(rc, seq_no, ADD_CMD, "route", err_str, err_rc);
651 int lustre_lnet_del_route(char *nw, char *gw,
652 int seq_no, struct cYAML **err_rc)
654 struct lnet_ioctl_config_data data;
655 lnet_nid_t gateway_nid;
656 int rc = LUSTRE_CFG_RC_NO_ERR;
657 __u32 net = LNET_NIDNET(LNET_NID_ANY);
658 char err_str[LNET_MAX_STR_LEN];
660 snprintf(err_str, sizeof(err_str), "\"Success\"");
662 if (nw == NULL || gw == NULL) {
665 "\"missing mandatory parameter(s): '%s'\"",
666 (nw == NULL && gw == NULL) ? "network, gateway" :
667 (nw == NULL) ? "network" : "gateway");
668 rc = LUSTRE_CFG_RC_MISSING_PARAM;
672 net = libcfs_str2net(nw);
673 if (net == LNET_NIDNET(LNET_NID_ANY)) {
676 "\"cannot parse net '%s'\"", nw);
677 rc = LUSTRE_CFG_RC_BAD_PARAM;
681 if (LNET_NETTYP(net) == CIBLND ||
682 LNET_NETTYP(net) == OPENIBLND ||
683 LNET_NETTYP(net) == IIBLND ||
684 LNET_NETTYP(net) == VIBLND) {
687 "\"obselete LNet type '%s'\"", libcfs_lnd2str(net));
688 rc = LUSTRE_CFG_RC_BAD_PARAM;
692 gateway_nid = libcfs_str2nid(gw);
693 if (gateway_nid == LNET_NID_ANY) {
696 "\"cannot parse gateway NID '%s'\"", gw);
697 rc = LUSTRE_CFG_RC_BAD_PARAM;
701 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
703 data.cfg_nid = gateway_nid;
705 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
710 "\"cannot delete route: %s\"", strerror(errno));
715 cYAML_build_error(rc, seq_no, DEL_CMD, "route", err_str, err_rc);
720 int lustre_lnet_show_route(char *nw, char *gw, int hops, int prio, int detail,
721 int seq_no, struct cYAML **show_rc,
722 struct cYAML **err_rc)
724 struct lnet_ioctl_config_data data;
725 lnet_nid_t gateway_nid;
726 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
728 __u32 net = LNET_NIDNET(LNET_NID_ANY);
730 struct cYAML *root = NULL, *route = NULL, *item = NULL;
731 struct cYAML *first_seq = NULL;
732 char err_str[LNET_MAX_STR_LEN];
735 snprintf(err_str, sizeof(err_str),
736 "\"out of memory\"");
739 net = libcfs_str2net(nw);
740 if (net == LNET_NIDNET(LNET_NID_ANY)) {
743 "\"cannot parse net '%s'\"", nw);
744 rc = LUSTRE_CFG_RC_BAD_PARAM;
748 if (LNET_NETTYP(net) == CIBLND ||
749 LNET_NETTYP(net) == OPENIBLND ||
750 LNET_NETTYP(net) == IIBLND ||
751 LNET_NETTYP(net) == VIBLND) {
754 "\"obsolete LNet type '%s'\"",
755 libcfs_lnd2str(net));
756 rc = LUSTRE_CFG_RC_BAD_PARAM;
760 /* show all routes without filtering on net */
761 net = LNET_NIDNET(LNET_NID_ANY);
765 gateway_nid = libcfs_str2nid(gw);
766 if (gateway_nid == LNET_NID_ANY) {
769 "\"cannot parse gateway NID '%s'\"", gw);
770 rc = LUSTRE_CFG_RC_BAD_PARAM;
774 /* show all routes with out filtering on gateway */
775 gateway_nid = LNET_NID_ANY;
777 if ((hops < 1 && hops != -1) || hops > 255) {
780 "\"invalid hop count %d, must be between 0 and 256\"",
782 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
786 /* create struct cYAML root object */
787 root = cYAML_create_object(NULL, NULL);
791 route = cYAML_create_seq(root, "route");
796 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
799 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
805 /* filter on provided data */
806 if (net != LNET_NIDNET(LNET_NID_ANY) &&
810 if (gateway_nid != LNET_NID_ANY &&
811 gateway_nid != data.cfg_nid)
815 hops != data.cfg_config_u.cfg_route.rtr_hop)
819 prio != data.cfg_config_u.cfg_route.rtr_priority)
822 /* default rc to -1 incase we hit the goto */
826 item = cYAML_create_seq_item(route);
830 if (first_seq == NULL)
833 if (cYAML_create_string(item, "net",
834 libcfs_net2str(data.cfg_net)) == NULL)
837 if (cYAML_create_string(item, "gateway",
838 libcfs_nid2str(data.cfg_nid)) == NULL)
842 if (cYAML_create_number(item, "hop",
843 data.cfg_config_u.cfg_route.
848 if (cYAML_create_number(item, "priority",
850 cfg_route.rtr_priority) == NULL)
853 if (cYAML_create_string(item, "state",
854 data.cfg_config_u.cfg_route.
856 "up" : "down") == NULL)
861 /* print output iff show_rc is not provided */
863 cYAML_print_tree(root);
865 if (l_errno != ENOENT) {
868 "\"cannot get routes: %s\"",
873 rc = LUSTRE_CFG_RC_NO_ERR;
875 snprintf(err_str, sizeof(err_str), "\"success\"");
877 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
878 cYAML_free_tree(root);
879 } else if (show_rc != NULL && *show_rc != NULL) {
880 struct cYAML *show_node;
881 /* find the route node, if one doesn't exist then
882 * insert one. Otherwise add to the one there
884 show_node = cYAML_get_object_item(*show_rc, "route");
885 if (show_node != NULL && cYAML_is_sequence(show_node)) {
886 cYAML_insert_child(show_node, first_seq);
889 } else if (show_node == NULL) {
890 cYAML_insert_sibling((*show_rc)->cy_child,
894 cYAML_free_tree(root);
900 cYAML_build_error(rc, seq_no, SHOW_CMD, "route", err_str, err_rc);
905 static int socket_intf_query(int request, char *intf,
911 if (strlen(intf) >= IFNAMSIZ || ifr == NULL)
912 return LUSTRE_CFG_RC_BAD_PARAM;
914 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
916 return LUSTRE_CFG_RC_BAD_PARAM;
918 strcpy(ifr->ifr_name, intf);
919 rc = ioctl(sockfd, request, ifr);
921 return LUSTRE_CFG_RC_BAD_PARAM;
927 * for each interface in the array of interfaces find the IP address of
928 * that interface, create its nid and add it to an array of NIDs.
929 * Stop if any of the interfaces is down
931 static int lustre_lnet_intf2nids(struct lnet_dlc_network_descr *nw,
932 lnet_nid_t **nids, __u32 *nnids)
934 int i = 0, count = 0, rc;
937 struct lnet_dlc_intf_descr *intf;
939 if (nw == NULL || nids == NULL)
940 return LUSTRE_CFG_RC_BAD_PARAM;
942 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network)
945 *nids = calloc(count, sizeof(lnet_nid_t));
947 return LUSTRE_CFG_RC_OUT_OF_MEM;
949 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network) {
950 memset(&ifr, 0, sizeof(ifr));
951 rc = socket_intf_query(SIOCGIFFLAGS, intf->intf_name, &ifr);
955 if ((ifr.ifr_flags & IFF_UP) == 0) {
956 rc = LUSTRE_CFG_RC_BAD_PARAM;
960 memset(&ifr, 0, sizeof(ifr));
961 rc = socket_intf_query(SIOCGIFADDR, intf->intf_name, &ifr);
965 ip = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
967 (*nids)[i] = LNET_MKNID(nw->nw_id, ip);
982 * called repeatedly until a match or no more ip range
984 * ip_range expression
985 * interface list with all the interface names.
986 * all the interfaces in the system.
988 * try to match the ip_range expr to one of the interfaces' IPs in
989 * the system. If we hit a patch for an interface. Check if that
990 * interface name is in the list.
992 * If there are more than one interface in the list, then make sure
993 * that the IPs for all of these interfaces match the ip ranges
996 * for each interface in intf_list
997 * look up the intf name in ifa
998 * if not there then no match
999 * check ip obtained from ifa against a match to any of the
1001 * If no match, then fail
1003 * The result is that all the interfaces have to match.
1005 int lustre_lnet_match_ip_to_intf(struct ifaddrs *ifa,
1006 struct list_head *intf_list,
1007 struct list_head *ip_ranges)
1011 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1012 struct ifaddrs *ifaddr = ifa;
1013 struct lustre_lnet_ip_range_descr *ip_range;
1017 * if there are no explicit interfaces, and no ip ranges, then
1018 * configure the first tcp interface we encounter.
1020 if (list_empty(intf_list) && list_empty(ip_ranges)) {
1021 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1022 if (ifaddr->ifa_addr == NULL)
1025 if ((ifaddr->ifa_flags & IFF_UP) == 0)
1028 family = ifaddr->ifa_addr->sa_family;
1029 if (family == AF_INET &&
1030 strcmp(ifaddr->ifa_name, "lo") != 0) {
1031 rc = lustre_lnet_add_intf_descr
1032 (intf_list, ifaddr->ifa_name,
1033 strlen(ifaddr->ifa_name));
1035 if (rc != LUSTRE_CFG_RC_NO_ERR)
1038 return LUSTRE_CFG_RC_MATCH;
1041 return LUSTRE_CFG_RC_NO_MATCH;
1045 * First interface which matches an IP pattern will be used
1047 if (list_empty(intf_list)) {
1049 * no interfaces provided in the rule, but an ip range is
1050 * provided, so try and match an interface to the ip
1053 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1054 if (ifaddr->ifa_addr == NULL)
1057 if ((ifaddr->ifa_flags & IFF_UP) == 0)
1060 family = ifaddr->ifa_addr->sa_family;
1061 if (family == AF_INET) {
1062 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->
1065 list_for_each_entry(ip_range, ip_ranges,
1067 rc = cfs_ip_addr_match(bswap_32(ip),
1068 &ip_range->ipr_expr);
1072 rc = lustre_lnet_add_intf_descr
1073 (intf_list, ifaddr->ifa_name,
1074 strlen(ifaddr->ifa_name));
1076 if (rc != LUSTRE_CFG_RC_NO_ERR)
1082 if (!list_empty(intf_list))
1083 return LUSTRE_CFG_RC_MATCH;
1085 return LUSTRE_CFG_RC_NO_MATCH;
1089 * If an interface is explicitly specified the ip-range might or
1090 * might not be specified. if specified the interface needs to match the
1091 * ip-range. If no ip-range then the interfaces are
1092 * automatically matched if they are all up.
1093 * If > 1 interfaces all the interfaces must match for the NI to
1096 list_for_each_entry_safe(intf_descr, tmp, intf_list, intf_on_network) {
1097 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1098 if (ifaddr->ifa_addr == NULL)
1101 family = ifaddr->ifa_addr->sa_family;
1102 if (family == AF_INET &&
1103 strcmp(intf_descr->intf_name,
1104 ifaddr->ifa_name) == 0)
1108 if (ifaddr == NULL) {
1109 list_del(&intf_descr->intf_on_network);
1110 free_intf_descr(intf_descr);
1114 if ((ifaddr->ifa_flags & IFF_UP) == 0) {
1115 list_del(&intf_descr->intf_on_network);
1116 free_intf_descr(intf_descr);
1120 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr.s_addr;
1123 list_for_each_entry(ip_range, ip_ranges, ipr_entry) {
1124 rc = cfs_ip_addr_match(bswap_32(ip), &ip_range->ipr_expr);
1130 /* no match for this interface */
1131 list_del(&intf_descr->intf_on_network);
1132 free_intf_descr(intf_descr);
1136 return LUSTRE_CFG_RC_MATCH;
1139 int lustre_lnet_resolve_ip2nets_rule(struct lustre_lnet_ip2nets *ip2nets,
1140 lnet_nid_t **nids, __u32 *nnids)
1142 struct ifaddrs *ifa;
1143 int rc = LUSTRE_CFG_RC_NO_ERR;
1145 rc = getifaddrs(&ifa);
1149 rc = lustre_lnet_match_ip_to_intf(ifa,
1150 &ip2nets->ip2nets_net.nw_intflist,
1151 &ip2nets->ip2nets_ip_ranges);
1152 if (rc != LUSTRE_CFG_RC_MATCH) {
1157 rc = lustre_lnet_intf2nids(&ip2nets->ip2nets_net, nids, nnids);
1158 if (rc != LUSTRE_CFG_RC_NO_ERR) {
1169 lustre_lnet_ioctl_config_ni(struct list_head *intf_list,
1170 struct lnet_ioctl_config_lnd_tunables *tunables,
1171 struct cfs_expr_list *global_cpts,
1172 lnet_nid_t *nids, char *err_str)
1175 struct lnet_ioctl_config_ni *conf;
1176 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1177 int rc = LUSTRE_CFG_RC_NO_ERR, i = 0;
1180 struct lnet_dlc_intf_descr *intf_descr;
1182 struct cfs_expr_list *cpt_expr;
1184 list_for_each_entry(intf_descr, intf_list,
1186 if (i == 0 && tunables != NULL)
1187 len = sizeof(struct lnet_ioctl_config_ni) +
1188 sizeof(struct lnet_ioctl_config_lnd_tunables);
1190 len = sizeof(struct lnet_ioctl_config_ni);
1192 data = calloc(1, len);
1193 conf = (struct lnet_ioctl_config_ni*) data;
1194 if (i == 0 && tunables != NULL)
1195 tun = (struct lnet_ioctl_config_lnd_tunables*)
1198 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1199 conf->lic_cfg_hdr.ioc_len = len;
1200 conf->lic_nid = nids[i];
1201 strncpy(conf->lic_ni_intf[0], intf_descr->intf_name,
1204 if (intf_descr->cpt_expr != NULL)
1205 cpt_expr = intf_descr->cpt_expr;
1206 else if (global_cpts != NULL)
1207 cpt_expr = global_cpts;
1211 if (cpt_expr != NULL) {
1212 count = cfs_expr_list_values(cpt_expr,
1213 LNET_MAX_SHOW_NUM_CPT,
1216 memcpy(conf->lic_cpts, cpt_array,
1217 sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1226 conf->lic_ncpts = count;
1228 if (i == 0 && tunables != NULL)
1229 /* TODO put in the LND tunables */
1230 memcpy(tun, tunables, sizeof(*tunables));
1232 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1237 "\"cannot add network: %s\"", strerror(errno));
1243 return LUSTRE_CFG_RC_NO_ERR;
1247 lustre_lnet_config_ip2nets(struct lustre_lnet_ip2nets *ip2nets,
1248 struct lnet_ioctl_config_lnd_tunables *tunables,
1249 struct cfs_expr_list *global_cpts,
1250 int seq_no, struct cYAML **err_rc)
1252 lnet_nid_t *nids = NULL;
1255 char err_str[LNET_MAX_STR_LEN];
1257 snprintf(err_str, sizeof(err_str), "\"success\"");
1262 "\"incomplete ip2nets information\"");
1263 rc = LUSTRE_CFG_RC_BAD_PARAM;
1267 rc = lustre_lnet_resolve_ip2nets_rule(ip2nets, &nids, &nnids);
1268 if (rc != LUSTRE_CFG_RC_NO_ERR && rc != LUSTRE_CFG_RC_MATCH) {
1271 "\"cannot resolve ip2nets rule\"");
1275 if (list_empty(&ip2nets->ip2nets_net.nw_intflist)) {
1276 snprintf(err_str, sizeof(err_str),
1277 "\"no interfaces match ip2nets rules\"");
1281 rc = lustre_lnet_ioctl_config_ni(&ip2nets->ip2nets_net.nw_intflist,
1282 tunables, global_cpts, nids,
1284 if (rc != LUSTRE_CFG_RC_NO_ERR)
1288 cYAML_build_error(rc, seq_no, ADD_CMD, "ip2nets", err_str, err_rc);
1292 int lustre_lnet_config_ni(struct lnet_dlc_network_descr *nw_descr,
1293 struct cfs_expr_list *global_cpts,
1295 struct lnet_ioctl_config_lnd_tunables *tunables,
1296 int seq_no, struct cYAML **err_rc)
1299 struct lnet_ioctl_config_ni *conf;
1300 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1301 char buf[LNET_MAX_STR_LEN];
1302 int rc = LUSTRE_CFG_RC_NO_ERR;
1303 char err_str[LNET_MAX_STR_LEN];
1304 lnet_nid_t *nids = NULL;
1308 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1311 snprintf(err_str, sizeof(err_str), "\"success\"");
1313 if (ip2net == NULL && nw_descr == NULL) {
1316 "\"mandatory parameters not specified.\"");
1317 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1321 if (ip2net != NULL && strlen(ip2net) >= sizeof(buf)) {
1324 "\"ip2net string too long %d\"",
1325 (int)strlen(ip2net));
1326 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1330 if (ip2net != NULL) {
1331 if (tunables != NULL)
1332 len = sizeof(struct lnet_ioctl_config_ni) +
1333 sizeof(struct lnet_ioctl_config_lnd_tunables);
1335 len = sizeof(struct lnet_ioctl_config_ni);
1336 data = calloc(1, len);
1337 conf = (struct lnet_ioctl_config_ni*) data;
1338 if (tunables != NULL)
1339 tun = (struct lnet_ioctl_config_lnd_tunables*)
1340 (data + sizeof(*conf));
1342 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1343 conf->lic_cfg_hdr.ioc_len = len;
1344 strncpy(conf->lic_legacy_ip2nets, ip2net,
1347 if (global_cpts != NULL) {
1348 count = cfs_expr_list_values(global_cpts,
1349 LNET_MAX_SHOW_NUM_CPT,
1352 memcpy(conf->lic_cpts, cpt_array,
1353 sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1362 conf->lic_ncpts = count;
1364 if (tunables != NULL)
1365 memcpy(tun, tunables, sizeof(*tunables));
1367 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1372 "\"cannot add network: %s\"", strerror(errno));
1379 if (LNET_NETTYP(nw_descr->nw_id) == LOLND)
1380 return LUSTRE_CFG_RC_NO_ERR;
1382 if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1385 "\"cannot parse net '%s'\"",
1386 libcfs_net2str(nw_descr->nw_id));
1387 rc = LUSTRE_CFG_RC_BAD_PARAM;
1391 if (list_empty(&nw_descr->nw_intflist)) {
1394 "\"no interface name provided\"");
1395 rc = LUSTRE_CFG_RC_BAD_PARAM;
1399 rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids);
1401 snprintf(err_str, sizeof(err_str),
1402 "\"bad parameter\"");
1403 rc = LUSTRE_CFG_RC_BAD_PARAM;
1407 rc = lustre_lnet_ioctl_config_ni(&nw_descr->nw_intflist,
1408 tunables, global_cpts, nids,
1412 if (nw_descr != NULL) {
1413 list_for_each_entry_safe(intf_descr, tmp,
1414 &nw_descr->nw_intflist,
1416 list_del(&intf_descr->intf_on_network);
1417 free_intf_descr(intf_descr);
1421 cYAML_build_error(rc, seq_no, ADD_CMD, "net", err_str, err_rc);
1432 int lustre_lnet_del_ni(struct lnet_dlc_network_descr *nw_descr,
1433 int seq_no, struct cYAML **err_rc)
1435 struct lnet_ioctl_config_ni data;
1436 int rc = LUSTRE_CFG_RC_NO_ERR, i;
1437 char err_str[LNET_MAX_STR_LEN];
1438 lnet_nid_t *nids = NULL;
1440 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1442 if (LNET_NETTYP(nw_descr->nw_id) == LOLND)
1443 return LUSTRE_CFG_RC_NO_ERR;
1445 snprintf(err_str, sizeof(err_str), "\"success\"");
1447 if (nw_descr == NULL) {
1450 "\"missing mandatory parameter\"");
1451 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1455 if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1458 "\"cannot parse net '%s'\"",
1459 libcfs_net2str(nw_descr->nw_id));
1460 rc = LUSTRE_CFG_RC_BAD_PARAM;
1464 rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids);
1466 snprintf(err_str, sizeof(err_str),
1467 "\"bad parameter\"");
1468 rc = LUSTRE_CFG_RC_BAD_PARAM;
1473 * no interfaces just the nw_id is specified
1476 nids = calloc(1, sizeof(*nids));
1478 snprintf(err_str, sizeof(err_str),
1479 "\"out of memory\"");
1480 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1483 nids[0] = LNET_MKNID(nw_descr->nw_id, 0);
1487 for (i = 0; i < nnids; i++) {
1488 LIBCFS_IOC_INIT_V2(data, lic_cfg_hdr);
1489 data.lic_nid = nids[i];
1491 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_LOCAL_NI, &data);
1496 "\"cannot del network: %s\"", strerror(errno));
1500 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
1502 list_del(&intf_descr->intf_on_network);
1503 free_intf_descr(intf_descr);
1507 cYAML_build_error(rc, seq_no, DEL_CMD, "net", err_str, err_rc);
1515 int lustre_lnet_show_net(char *nw, int detail, int seq_no,
1516 struct cYAML **show_rc, struct cYAML **err_rc)
1519 struct lnet_ioctl_config_ni *ni_data;
1520 struct lnet_ioctl_config_lnd_tunables *lnd;
1521 struct lnet_ioctl_element_stats *stats;
1522 __u32 net = LNET_NIDNET(LNET_NID_ANY);
1523 __u32 prev_net = LNET_NIDNET(LNET_NID_ANY);
1524 int rc = LUSTRE_CFG_RC_OUT_OF_MEM, i, j;
1526 struct cYAML *root = NULL, *tunables = NULL,
1527 *net_node = NULL, *interfaces = NULL,
1528 *item = NULL, *first_seq = NULL,
1529 *tmp = NULL, *statistics = NULL;
1530 int str_buf_len = LNET_MAX_SHOW_NUM_CPT * 2;
1531 char str_buf[str_buf_len];
1533 char err_str[LNET_MAX_STR_LEN];
1534 bool exist = false, new_net = true;
1536 size_t buf_size = sizeof(*ni_data) + sizeof(*lnd) + sizeof(*stats);
1538 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1540 buf = calloc(1, buf_size);
1544 ni_data = (struct lnet_ioctl_config_ni *)buf;
1547 net = libcfs_str2net(nw);
1548 if (net == LNET_NIDNET(LNET_NID_ANY)) {
1551 "\"cannot parse net '%s'\"", nw);
1552 rc = LUSTRE_CFG_RC_BAD_PARAM;
1557 root = cYAML_create_object(NULL, NULL);
1561 net_node = cYAML_create_seq(root, "net");
1562 if (net_node == NULL)
1569 memset(buf, 0, buf_size);
1571 LIBCFS_IOC_INIT_V2(*ni_data, lic_cfg_hdr);
1573 * set the ioc_len to the proper value since INIT assumes
1576 ni_data->lic_cfg_hdr.ioc_len = buf_size;
1577 ni_data->lic_idx = i;
1579 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LOCAL_NI, ni_data);
1585 rc_net = LNET_NIDNET(ni_data->lic_nid);
1587 /* filter on provided data */
1588 if (net != LNET_NIDNET(LNET_NID_ANY) &&
1592 /* default rc to -1 in case we hit the goto */
1596 stats = (struct lnet_ioctl_element_stats *)ni_data->lic_bulk;
1597 lnd = (struct lnet_ioctl_config_lnd_tunables *)
1598 (ni_data->lic_bulk + sizeof(*stats));
1600 if (rc_net != prev_net) {
1607 if (!cYAML_create_string(net_node, "net type",
1608 libcfs_net2str(rc_net)))
1611 tmp = cYAML_create_seq(net_node, "local NI(s)");
1617 /* create the tree to be printed. */
1618 item = cYAML_create_seq_item(tmp);
1622 if (first_seq == NULL)
1625 if (cYAML_create_string(item, "nid",
1626 libcfs_nid2str(ni_data->lic_nid)) == NULL)
1629 if (cYAML_create_string(item,
1631 (ni_data->lic_status ==
1632 LNET_NI_STATUS_UP) ?
1633 "up" : "down") == NULL)
1636 /* don't add interfaces unless there is at least one
1638 if (strlen(ni_data->lic_ni_intf[0]) > 0) {
1639 interfaces = cYAML_create_object(item, "interfaces");
1640 if (interfaces == NULL)
1643 for (j = 0; j < LNET_MAX_INTERFACES; j++) {
1644 if (strlen(ni_data->lic_ni_intf[j]) > 0) {
1646 sizeof(str_buf), "%d", j);
1647 if (cYAML_create_string(interfaces,
1649 ni_data->lic_ni_intf[j]) ==
1659 statistics = cYAML_create_object(item, "statistics");
1660 if (statistics == NULL)
1663 if (cYAML_create_number(statistics, "send_count",
1668 if (cYAML_create_number(statistics, "recv_count",
1673 if (cYAML_create_number(statistics, "drop_count",
1678 tunables = cYAML_create_object(item, "tunables");
1682 rc = lustre_net_show_tunables(tunables, &lnd->lt_cmn);
1683 if (rc != LUSTRE_CFG_RC_NO_ERR)
1686 tunables = cYAML_create_object(item, "lnd tunables");
1687 if (tunables == NULL)
1690 rc = lustre_ni_show_tunables(tunables, LNET_NETTYP(rc_net),
1692 if (rc != LUSTRE_CFG_RC_NO_ERR)
1695 if (cYAML_create_number(item, "tcp bonding",
1696 ni_data->lic_tcp_bonding)
1700 if (cYAML_create_number(item, "dev cpt",
1701 ni_data->lic_dev_cpt) == NULL)
1704 /* out put the CPTs in the format: "[x,x,x,...]" */
1705 limit = str_buf + str_buf_len - 3;
1706 pos += snprintf(pos, limit - pos, "\"[");
1707 for (j = 0 ; ni_data->lic_ncpts >= 1 &&
1708 j < ni_data->lic_ncpts &&
1710 pos += snprintf(pos, limit - pos,
1711 "%d", ni_data->lic_cpts[j]);
1712 if ((j + 1) < ni_data->lic_ncpts)
1713 pos += snprintf(pos, limit - pos, ",");
1715 pos += snprintf(pos, 3, "]\"");
1717 if (ni_data->lic_ncpts >= 1 &&
1718 cYAML_create_string(item, "CPT",
1724 /* Print out the net information only if show_rc is not provided */
1725 if (show_rc == NULL)
1726 cYAML_print_tree(root);
1728 if (l_errno != ENOENT) {
1731 "\"cannot get networks: %s\"",
1736 rc = LUSTRE_CFG_RC_NO_ERR;
1738 snprintf(err_str, sizeof(err_str), "\"success\"");
1740 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
1741 cYAML_free_tree(root);
1742 } else if (show_rc != NULL && *show_rc != NULL) {
1743 struct cYAML *show_node;
1744 /* find the net node, if one doesn't exist
1745 * then insert one. Otherwise add to the one there
1747 show_node = cYAML_get_object_item(*show_rc, "net");
1748 if (show_node != NULL && cYAML_is_sequence(show_node)) {
1749 cYAML_insert_child(show_node, first_seq);
1752 } else if (show_node == NULL) {
1753 cYAML_insert_sibling((*show_rc)->cy_child,
1757 cYAML_free_tree(root);
1763 cYAML_build_error(rc, seq_no, SHOW_CMD, "net", err_str, err_rc);
1768 int lustre_lnet_enable_routing(int enable, int seq_no, struct cYAML **err_rc)
1770 struct lnet_ioctl_config_data data;
1771 int rc = LUSTRE_CFG_RC_NO_ERR;
1772 char err_str[LNET_MAX_STR_LEN];
1774 snprintf(err_str, sizeof(err_str), "\"success\"");
1776 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1777 data.cfg_config_u.cfg_buffers.buf_enable = (enable) ? 1 : 0;
1779 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIG_RTR, &data);
1784 "\"cannot %s routing %s\"",
1785 (enable) ? "enable" : "disable", strerror(errno));
1790 cYAML_build_error(rc, seq_no,
1791 (enable) ? ADD_CMD : DEL_CMD,
1792 "routing", err_str, err_rc);
1797 int lustre_lnet_config_numa_range(int range, int seq_no, struct cYAML **err_rc)
1799 struct lnet_ioctl_numa_range data;
1800 int rc = LUSTRE_CFG_RC_NO_ERR;
1801 char err_str[LNET_MAX_STR_LEN];
1803 snprintf(err_str, sizeof(err_str), "\"success\"");
1808 "\"range must be >= 0\"");
1809 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1813 LIBCFS_IOC_INIT_V2(data, nr_hdr);
1814 data.nr_range = range;
1816 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_SET_NUMA_RANGE, &data);
1821 "\"cannot configure buffers: %s\"", strerror(errno));
1826 cYAML_build_error(rc, seq_no, ADD_CMD, "numa_range", err_str, err_rc);
1831 int lustre_lnet_config_buffers(int tiny, int small, int large, int seq_no,
1832 struct cYAML **err_rc)
1834 struct lnet_ioctl_config_data data;
1835 int rc = LUSTRE_CFG_RC_NO_ERR;
1836 char err_str[LNET_MAX_STR_LEN];
1838 snprintf(err_str, sizeof(err_str), "\"success\"");
1840 /* -1 indicates to ignore changes to this field */
1841 if (tiny < -1 || small < -1 || large < -1) {
1844 "\"tiny, small and large must be >= 0\"");
1845 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1849 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1850 data.cfg_config_u.cfg_buffers.buf_tiny = tiny;
1851 data.cfg_config_u.cfg_buffers.buf_small = small;
1852 data.cfg_config_u.cfg_buffers.buf_large = large;
1854 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_BUF, &data);
1859 "\"cannot configure buffers: %s\"", strerror(errno));
1864 cYAML_build_error(rc, seq_no, ADD_CMD, "buf", err_str, err_rc);
1869 int lustre_lnet_show_routing(int seq_no, struct cYAML **show_rc,
1870 struct cYAML **err_rc)
1872 struct lnet_ioctl_config_data *data;
1873 struct lnet_ioctl_pool_cfg *pool_cfg = NULL;
1874 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1877 char *pools[LNET_NRBPOOLS] = {"tiny", "small", "large"};
1878 int buf_count[LNET_NRBPOOLS] = {0};
1879 struct cYAML *root = NULL, *pools_node = NULL,
1880 *type_node = NULL, *item = NULL, *cpt = NULL,
1881 *first_seq = NULL, *buffers = NULL;
1883 char err_str[LNET_MAX_STR_LEN];
1884 char node_name[LNET_MAX_STR_LEN];
1887 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1889 buf = calloc(1, sizeof(*data) + sizeof(*pool_cfg));
1893 data = (struct lnet_ioctl_config_data *)buf;
1895 root = cYAML_create_object(NULL, NULL);
1899 pools_node = cYAML_create_seq(root, "routing");
1900 if (pools_node == NULL)
1904 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
1905 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
1906 sizeof(struct lnet_ioctl_pool_cfg);
1907 data->cfg_count = i;
1909 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_BUF, data);
1917 pool_cfg = (struct lnet_ioctl_pool_cfg *)data->cfg_bulk;
1919 snprintf(node_name, sizeof(node_name), "cpt[%d]", i);
1920 item = cYAML_create_seq_item(pools_node);
1924 if (first_seq == NULL)
1927 cpt = cYAML_create_object(item, node_name);
1931 /* create the tree and print */
1932 for (j = 0; j < LNET_NRBPOOLS; j++) {
1933 type_node = cYAML_create_object(cpt, pools[j]);
1934 if (type_node == NULL)
1936 if (cYAML_create_number(type_node, "npages",
1937 pool_cfg->pl_pools[j].pl_npages)
1940 if (cYAML_create_number(type_node, "nbuffers",
1941 pool_cfg->pl_pools[j].
1942 pl_nbuffers) == NULL)
1944 if (cYAML_create_number(type_node, "credits",
1945 pool_cfg->pl_pools[j].
1946 pl_credits) == NULL)
1948 if (cYAML_create_number(type_node, "mincredits",
1949 pool_cfg->pl_pools[j].
1950 pl_mincredits) == NULL)
1952 /* keep track of the total count for each of the
1953 * tiny, small and large buffers */
1954 buf_count[j] += pool_cfg->pl_pools[j].pl_nbuffers;
1958 if (pool_cfg != NULL) {
1959 item = cYAML_create_seq_item(pools_node);
1963 if (cYAML_create_number(item, "enable", pool_cfg->pl_routing) ==
1968 /* create a buffers entry in the show. This is necessary so that
1969 * if the YAML output is used to configure a node, the buffer
1970 * configuration takes hold */
1971 buffers = cYAML_create_object(root, "buffers");
1972 if (buffers == NULL)
1975 for (i = 0; i < LNET_NRBPOOLS; i++) {
1976 if (cYAML_create_number(buffers, pools[i], buf_count[i]) == NULL)
1980 if (show_rc == NULL)
1981 cYAML_print_tree(root);
1983 if (l_errno != ENOENT) {
1986 "\"cannot get routing information: %s\"",
1991 rc = LUSTRE_CFG_RC_NO_ERR;
1993 snprintf(err_str, sizeof(err_str), "\"success\"");
1994 rc = LUSTRE_CFG_RC_NO_ERR;
1998 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
1999 cYAML_free_tree(root);
2000 } else if (show_rc != NULL && *show_rc != NULL) {
2001 struct cYAML *routing_node;
2002 /* there should exist only one routing block and one
2003 * buffers block. If there already exists a previous one
2004 * then don't add another */
2005 routing_node = cYAML_get_object_item(*show_rc, "routing");
2006 if (routing_node == NULL) {
2007 cYAML_insert_sibling((*show_rc)->cy_child,
2011 cYAML_free_tree(root);
2017 cYAML_build_error(rc, seq_no, SHOW_CMD, "routing", err_str, err_rc);
2022 int lustre_lnet_show_peer(char *knid, int detail, int seq_no,
2023 struct cYAML **show_rc, struct cYAML **err_rc)
2026 * TODO: This function is changing in a future patch to accommodate
2027 * PEER_LIST and proper filtering on any nid of the peer
2029 struct lnet_ioctl_peer_cfg *peer_info;
2030 struct lnet_peer_ni_credit_info *lpni_cri;
2031 struct lnet_ioctl_element_stats *lpni_stats;
2032 int rc = LUSTRE_CFG_RC_OUT_OF_MEM, ncpt = 0, i = 0, j = 0;
2034 struct cYAML *root = NULL, *peer = NULL, *peer_ni = NULL,
2035 *first_seq = NULL, *peer_root = NULL, *tmp = NULL;
2036 char err_str[LNET_MAX_STR_LEN];
2037 lnet_nid_t prev_primary_nid = LNET_NID_ANY, primary_nid = LNET_NID_ANY;
2038 int data_size = sizeof(*peer_info) + sizeof(*lpni_cri) +
2039 sizeof(*lpni_stats);
2040 char *data = calloc(data_size, 1);
2041 bool new_peer = true;
2043 snprintf(err_str, sizeof(err_str),
2044 "\"out of memory\"");
2049 peer_info = (struct lnet_ioctl_peer_cfg *)data;
2051 /* create struct cYAML root object */
2052 root = cYAML_create_object(NULL, NULL);
2056 peer_root = cYAML_create_seq(root, "peer");
2057 if (peer_root == NULL)
2061 primary_nid = libcfs_str2nid(knid);
2065 memset(data, 0, data_size);
2066 LIBCFS_IOC_INIT_V2(*peer_info, prcfg_hdr);
2067 peer_info->prcfg_hdr.ioc_len = data_size;
2068 peer_info->prcfg_idx = i;
2070 rc = l_ioctl(LNET_DEV_ID,
2071 IOC_LIBCFS_GET_PEER_NI, peer_info);
2077 if (primary_nid != LNET_NID_ANY &&
2078 primary_nid != peer_info->prcfg_prim_nid)
2081 lpni_cri = (struct lnet_peer_ni_credit_info*)peer_info->prcfg_bulk;
2082 lpni_stats = (struct lnet_ioctl_element_stats *)
2083 (peer_info->prcfg_bulk +
2086 peer = cYAML_create_seq_item(peer_root);
2090 if (peer_info->prcfg_prim_nid != prev_primary_nid) {
2091 prev_primary_nid = peer_info->prcfg_prim_nid;
2096 lnet_nid_t pnid = peer_info->prcfg_prim_nid;
2097 if (cYAML_create_string(peer, "primary nid",
2098 libcfs_nid2str(pnid))
2101 if (cYAML_create_string(peer, "Multi-Rail",
2102 peer_info->prcfg_mr ?
2106 tmp = cYAML_create_seq(peer, "peer ni");
2112 if (first_seq == NULL)
2115 peer_ni = cYAML_create_seq_item(tmp);
2116 if (peer_ni == NULL)
2119 if (cYAML_create_string(peer_ni, "nid",
2121 (peer_info->prcfg_cfg_nid))
2125 if (cYAML_create_string(peer_ni, "state",
2126 lpni_cri->cr_aliveness)
2133 if (cYAML_create_number(peer_ni, "max_ni_tx_credits",
2134 lpni_cri->cr_ni_peer_tx_credits)
2138 if (cYAML_create_number(peer_ni, "available_tx_credits",
2139 lpni_cri->cr_peer_tx_credits)
2143 if (cYAML_create_number(peer_ni, "min_tx_credits",
2144 lpni_cri->cr_peer_min_tx_credits)
2148 if (cYAML_create_number(peer_ni, "tx_q_num_of_buf",
2149 lpni_cri->cr_peer_tx_qnob)
2153 if (cYAML_create_number(peer_ni, "available_rtr_credits",
2154 lpni_cri->cr_peer_rtr_credits)
2158 if (cYAML_create_number(peer_ni, "min_rtr_credits",
2159 lpni_cri->cr_peer_min_rtr_credits)
2163 if (cYAML_create_number(peer_ni, "send_count",
2164 lpni_stats->send_count)
2168 if (cYAML_create_number(peer_ni, "recv_count",
2169 lpni_stats->recv_count)
2173 if (cYAML_create_number(peer_ni, "drop_count",
2174 lpni_stats->drop_count)
2178 if (cYAML_create_number(peer_ni, "refcount",
2179 lpni_cri->cr_refcount) == NULL)
2183 if (l_errno != ENOENT) {
2186 "\"cannot get peer information: %s\"",
2195 /* print output iff show_rc is not provided */
2196 if (show_rc == NULL)
2197 cYAML_print_tree(root);
2199 snprintf(err_str, sizeof(err_str), "\"success\"");
2200 rc = LUSTRE_CFG_RC_NO_ERR;
2203 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2204 cYAML_free_tree(root);
2205 } else if (show_rc != NULL && *show_rc != NULL) {
2206 struct cYAML *show_node;
2207 /* find the peer node, if one doesn't exist then
2208 * insert one. Otherwise add to the one there
2210 show_node = cYAML_get_object_item(*show_rc,
2212 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2213 cYAML_insert_child(show_node, first_seq);
2216 } else if (show_node == NULL) {
2217 cYAML_insert_sibling((*show_rc)->cy_child,
2221 cYAML_free_tree(root);
2227 cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
2233 int lustre_lnet_show_numa_range(int seq_no, struct cYAML **show_rc,
2234 struct cYAML **err_rc)
2236 struct lnet_ioctl_numa_range data;
2237 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2239 char err_str[LNET_MAX_STR_LEN];
2240 struct cYAML *root = NULL, *range = NULL;
2242 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2244 LIBCFS_IOC_INIT_V2(data, nr_hdr);
2246 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NUMA_RANGE, &data);
2251 "\"cannot get numa range: %s\"",
2257 root = cYAML_create_object(NULL, NULL);
2261 range = cYAML_create_object(root, "numa");
2265 if (cYAML_create_number(range, "range",
2266 data.nr_range) == NULL)
2269 if (show_rc == NULL)
2270 cYAML_print_tree(root);
2272 snprintf(err_str, sizeof(err_str), "\"success\"");
2274 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2275 cYAML_free_tree(root);
2276 } else if (show_rc != NULL && *show_rc != NULL) {
2277 cYAML_insert_sibling((*show_rc)->cy_child,
2284 cYAML_build_error(rc, seq_no, SHOW_CMD, "numa", err_str, err_rc);
2289 int lustre_lnet_show_stats(int seq_no, struct cYAML **show_rc,
2290 struct cYAML **err_rc)
2292 struct lnet_ioctl_lnet_stats data;
2293 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2295 char err_str[LNET_MAX_STR_LEN];
2296 struct cYAML *root = NULL, *stats = NULL;
2298 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2300 LIBCFS_IOC_INIT_V2(data, st_hdr);
2302 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LNET_STATS, &data);
2307 "\"cannot get lnet statistics: %s\"",
2313 root = cYAML_create_object(NULL, NULL);
2317 stats = cYAML_create_object(root, "statistics");
2321 if (cYAML_create_number(stats, "msgs_alloc",
2322 data.st_cntrs.msgs_alloc) == NULL)
2325 if (cYAML_create_number(stats, "msgs_max",
2326 data.st_cntrs.msgs_max) == NULL)
2329 if (cYAML_create_number(stats, "errors",
2330 data.st_cntrs.errors) == NULL)
2333 if (cYAML_create_number(stats, "send_count",
2334 data.st_cntrs.send_count) == NULL)
2337 if (cYAML_create_number(stats, "recv_count",
2338 data.st_cntrs.recv_count) == NULL)
2341 if (cYAML_create_number(stats, "route_count",
2342 data.st_cntrs.route_count) == NULL)
2345 if (cYAML_create_number(stats, "drop_count",
2346 data.st_cntrs.drop_count) == NULL)
2349 if (cYAML_create_number(stats, "send_length",
2350 data.st_cntrs.send_length) == NULL)
2353 if (cYAML_create_number(stats, "recv_length",
2354 data.st_cntrs.recv_length) == NULL)
2357 if (cYAML_create_number(stats, "route_length",
2358 data.st_cntrs.route_length) == NULL)
2361 if (cYAML_create_number(stats, "drop_length",
2362 data.st_cntrs.drop_length) == NULL)
2365 if (show_rc == NULL)
2366 cYAML_print_tree(root);
2368 snprintf(err_str, sizeof(err_str), "\"success\"");
2370 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2371 cYAML_free_tree(root);
2372 } else if (show_rc != NULL && *show_rc != NULL) {
2373 cYAML_insert_sibling((*show_rc)->cy_child,
2380 cYAML_build_error(rc, seq_no, SHOW_CMD, "statistics", err_str, err_rc);
2385 typedef int (*cmd_handler_t)(struct cYAML *tree,
2386 struct cYAML **show_rc,
2387 struct cYAML **err_rc);
2389 static int handle_yaml_config_route(struct cYAML *tree, struct cYAML **show_rc,
2390 struct cYAML **err_rc)
2392 struct cYAML *net, *gw, *hop, *prio, *seq_no;
2394 net = cYAML_get_object_item(tree, "net");
2395 gw = cYAML_get_object_item(tree, "gateway");
2396 hop = cYAML_get_object_item(tree, "hop");
2397 prio = cYAML_get_object_item(tree, "priority");
2398 seq_no = cYAML_get_object_item(tree, "seq_no");
2400 return lustre_lnet_config_route((net) ? net->cy_valuestring : NULL,
2401 (gw) ? gw->cy_valuestring : NULL,
2402 (hop) ? hop->cy_valueint : -1,
2403 (prio) ? prio->cy_valueint : -1,
2404 (seq_no) ? seq_no->cy_valueint : -1,
2408 static void yaml_free_string_array(char **array, int num)
2411 char **sub_array = array;
2413 for (i = 0; i < num; i++) {
2414 if (*sub_array != NULL)
2424 * 0: <intf_name>['['<expr>']']
2425 * 1: <intf_name>['['<expr>']']
2427 static int yaml_copy_intf_info(struct cYAML *intf_tree,
2428 struct lnet_dlc_network_descr *nw_descr)
2430 struct cYAML *child = NULL;
2431 int intf_num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
2432 struct lnet_dlc_intf_descr *intf_descr, *tmp;
2434 if (intf_tree == NULL || nw_descr == NULL)
2435 return LUSTRE_CFG_RC_BAD_PARAM;
2437 /* now grab all the interfaces and their cpts */
2438 child = intf_tree->cy_child;
2439 while (child != NULL) {
2440 if (child->cy_valuestring == NULL) {
2441 child = child->cy_next;
2445 if (strlen(child->cy_valuestring) >= LNET_MAX_STR_LEN)
2448 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist,
2449 child->cy_valuestring,
2450 strlen(child->cy_valuestring));
2451 if (rc != LUSTRE_CFG_RC_NO_ERR)
2455 child = child->cy_next;
2459 return LUSTRE_CFG_RC_MISSING_PARAM;
2464 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
2466 list_del(&intf_descr->intf_on_network);
2467 free_intf_descr(intf_descr);
2474 yaml_extract_cmn_tunables(struct cYAML *tree,
2475 struct lnet_ioctl_config_lnd_cmn_tunables *tunables,
2476 struct cfs_expr_list **global_cpts)
2478 struct cYAML *tun, *item, *smp;
2481 tun = cYAML_get_object_item(tree, "tunables");
2483 item = cYAML_get_object_item(tun, "peer_timeout");
2485 tunables->lct_peer_timeout = item->cy_valueint;
2486 item = cYAML_get_object_item(tun, "peer_credits");
2488 tunables->lct_peer_tx_credits = item->cy_valueint;
2489 item = cYAML_get_object_item(tun, "peer_buffer_credits");
2491 tunables->lct_peer_rtr_credits = item->cy_valueint;
2492 item = cYAML_get_object_item(tun, "credits");
2494 tunables->lct_max_tx_credits = item->cy_valueint;
2495 smp = cYAML_get_object_item(tun, "CPT");
2497 rc = cfs_expr_list_parse(smp->cy_valuestring,
2498 strlen(smp->cy_valuestring),
2499 0, UINT_MAX, global_cpts);
2501 *global_cpts = NULL;
2511 yaml_extract_tunables(struct cYAML *tree,
2512 struct lnet_ioctl_config_lnd_tunables *tunables,
2513 struct cfs_expr_list **global_cpts,
2518 rc = yaml_extract_cmn_tunables(tree, &tunables->lt_cmn,
2524 lustre_yaml_extract_lnd_tunables(tree, net_type,
2532 * - net type: <net>[<NUM>]
2534 * - nid: <ip>@<net>[<NUM>]
2537 * 0: <intf_name>['['<expr>']']
2538 * 1: <intf_name>['['<expr>']']
2540 * peer_timeout: <NUM>
2541 * peer_credits: <NUM>
2542 * peer_buffer_credits: <NUM>
2545 * peercredits_hiw: <NUM>
2546 * map_on_demand: <NUM>
2547 * concurrent_sends: <NUM>
2548 * fmr_pool_size: <NUM>
2549 * fmr_flush_trigger: <NUM>
2552 * At least one interface is required. If no interfaces are provided the
2553 * network interface can not be configured.
2555 static int handle_yaml_config_ni(struct cYAML *tree, struct cYAML **show_rc,
2556 struct cYAML **err_rc)
2558 struct cYAML *net, *intf, *seq_no, *ip2net = NULL, *local_nis = NULL,
2560 int num_entries = 0, rc;
2561 struct lnet_dlc_network_descr nw_descr;
2562 struct cfs_expr_list *global_cpts = NULL;
2563 struct lnet_ioctl_config_lnd_tunables tunables;
2566 memset(&tunables, 0, sizeof(tunables));
2568 INIT_LIST_HEAD(&nw_descr.network_on_rule);
2569 INIT_LIST_HEAD(&nw_descr.nw_intflist);
2571 ip2net = cYAML_get_object_item(tree, "ip2net");
2572 net = cYAML_get_object_item(tree, "net type");
2574 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
2577 * if neither net nor ip2nets are present, then we can not
2578 * configure the network.
2580 if (!net && !ip2net)
2581 return LUSTRE_CFG_RC_MISSING_PARAM;
2583 local_nis = cYAML_get_object_item(tree, "local NI(s)");
2584 if (local_nis == NULL)
2585 return LUSTRE_CFG_RC_MISSING_PARAM;
2587 if (!cYAML_is_sequence(local_nis))
2588 return LUSTRE_CFG_RC_BAD_PARAM;
2590 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
2591 intf = cYAML_get_object_item(item, "interfaces");
2594 num_entries = yaml_copy_intf_info(intf, &nw_descr);
2595 if (num_entries <= 0) {
2596 cYAML_build_error(num_entries, -1, "ni", "add",
2597 "bad interface list",
2599 return LUSTRE_CFG_RC_BAD_PARAM;
2603 found = yaml_extract_tunables(tree, &tunables, &global_cpts,
2604 LNET_NETTYP(nw_descr.nw_id));
2605 seq_no = cYAML_get_object_item(tree, "seq_no");
2607 rc = lustre_lnet_config_ni(&nw_descr,
2609 (ip2net) ? ip2net->cy_valuestring : NULL,
2610 (found) ? &tunables: NULL,
2611 (seq_no) ? seq_no->cy_valueint : -1,
2614 if (global_cpts != NULL)
2615 cfs_expr_list_free(global_cpts);
2622 * - net-spec: <tcp|o2ib|gni>[NUM]
2624 * 0: <intf name>['['<expr>']']
2625 * 1: <intf name>['['<expr>']']
2627 * 0: <expr.expr.expr.expr>
2628 * 1: <expr.expr.expr.expr>
2630 static int handle_yaml_config_ip2nets(struct cYAML *tree,
2631 struct cYAML **show_rc,
2632 struct cYAML **err_rc)
2634 struct cYAML *net, *ip_range, *item = NULL, *intf = NULL,
2636 struct lustre_lnet_ip2nets ip2nets;
2637 struct lustre_lnet_ip_range_descr *ip_range_descr = NULL,
2639 int rc = LUSTRE_CFG_RC_NO_ERR;
2640 struct cfs_expr_list *global_cpts = NULL;
2641 struct cfs_expr_list *el, *el_tmp;
2642 struct lnet_ioctl_config_lnd_tunables tunables;
2643 struct lnet_dlc_intf_descr *intf_descr, *intf_tmp;
2646 memset(&tunables, 0, sizeof(tunables));
2648 /* initialize all lists */
2649 INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
2650 INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
2651 INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
2653 net = cYAML_get_object_item(tree, "net-spec");
2655 return LUSTRE_CFG_RC_BAD_PARAM;
2657 if (net != NULL && net->cy_valuestring == NULL)
2658 return LUSTRE_CFG_RC_BAD_PARAM;
2660 /* assign the network id */
2661 ip2nets.ip2nets_net.nw_id = libcfs_str2net(net->cy_valuestring);
2662 if (ip2nets.ip2nets_net.nw_id == LNET_NID_ANY)
2663 return LUSTRE_CFG_RC_BAD_PARAM;
2665 seq_no = cYAML_get_object_item(tree, "seq_no");
2667 intf = cYAML_get_object_item(tree, "interfaces");
2669 rc = yaml_copy_intf_info(intf, &ip2nets.ip2nets_net);
2671 return LUSTRE_CFG_RC_BAD_PARAM;
2674 ip_range = cYAML_get_object_item(tree, "ip-range");
2675 if (ip_range != NULL) {
2676 item = ip_range->cy_child;
2677 while (item != NULL) {
2678 if (item->cy_valuestring == NULL) {
2679 item = item->cy_next;
2683 rc = lustre_lnet_add_ip_range(&ip2nets.ip2nets_ip_ranges,
2684 item->cy_valuestring);
2686 if (rc != LUSTRE_CFG_RC_NO_ERR)
2689 item = item->cy_next;
2693 found = yaml_extract_tunables(tree, &tunables, &global_cpts,
2694 LNET_NETTYP(ip2nets.ip2nets_net.nw_id));
2696 rc = lustre_lnet_config_ip2nets(&ip2nets,
2697 (found) ? &tunables : NULL,
2699 (seq_no) ? seq_no->cy_valueint : -1,
2703 * don't stop because there was no match. Continue processing the
2704 * rest of the rules. If non-match then nothing is configured
2706 if (rc == LUSTRE_CFG_RC_NO_MATCH)
2707 rc = LUSTRE_CFG_RC_NO_ERR;
2709 list_for_each_entry_safe(intf_descr, intf_tmp,
2710 &ip2nets.ip2nets_net.nw_intflist,
2712 list_del(&intf_descr->intf_on_network);
2713 free_intf_descr(intf_descr);
2716 list_for_each_entry_safe(ip_range_descr, tmp,
2717 &ip2nets.ip2nets_ip_ranges,
2719 list_del(&ip_range_descr->ipr_entry);
2720 list_for_each_entry_safe(el, el_tmp, &ip_range_descr->ipr_expr,
2722 list_del(&el->el_link);
2723 cfs_expr_list_free(el);
2725 free(ip_range_descr);
2731 static int handle_yaml_del_ni(struct cYAML *tree, struct cYAML **show_rc,
2732 struct cYAML **err_rc)
2734 struct cYAML *net = NULL, *intf = NULL, *seq_no = NULL, *item = NULL,
2736 int num_entries, rc;
2737 struct lnet_dlc_network_descr nw_descr;
2739 INIT_LIST_HEAD(&nw_descr.network_on_rule);
2740 INIT_LIST_HEAD(&nw_descr.nw_intflist);
2742 net = cYAML_get_object_item(tree, "net type");
2744 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
2746 local_nis = cYAML_get_object_item(tree, "local NI(s)");
2747 if (local_nis == NULL)
2748 return LUSTRE_CFG_RC_MISSING_PARAM;
2750 if (!cYAML_is_sequence(local_nis))
2751 return LUSTRE_CFG_RC_BAD_PARAM;
2753 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
2754 intf = cYAML_get_object_item(item, "interfaces");
2757 num_entries = yaml_copy_intf_info(intf, &nw_descr);
2758 if (num_entries <= 0) {
2759 cYAML_build_error(num_entries, -1, "ni", "add",
2760 "bad interface list",
2762 return LUSTRE_CFG_RC_BAD_PARAM;
2766 seq_no = cYAML_get_object_item(tree, "seq_no");
2768 rc = lustre_lnet_del_ni((net) ? &nw_descr : NULL,
2769 (seq_no) ? seq_no->cy_valueint : -1,
2775 static int yaml_copy_peer_nids(struct cYAML *tree, char ***nidsppp)
2777 struct cYAML *nids_entry = NULL, *child = NULL, *entry = NULL;
2779 int num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
2781 nids_entry = cYAML_get_object_item(tree, "peer ni");
2782 if (cYAML_is_sequence(nids_entry)) {
2783 while (cYAML_get_next_seq_item(nids_entry, &child))
2788 return LUSTRE_CFG_RC_MISSING_PARAM;
2790 nids = calloc(sizeof(*nids) * num, 1);
2792 return LUSTRE_CFG_RC_OUT_OF_MEM;
2794 /* now grab all the nids */
2797 while (cYAML_get_next_seq_item(nids_entry, &child)) {
2798 entry = cYAML_get_object_item(child, "nid");
2801 nids[num] = calloc(strlen(entry->cy_valuestring) + 1, 1);
2803 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2806 strncpy(nids[num], entry->cy_valuestring,
2807 strlen(entry->cy_valuestring));
2817 yaml_free_string_array(nids, num);
2822 static int handle_yaml_config_peer(struct cYAML *tree, struct cYAML **show_rc,
2823 struct cYAML **err_rc)
2827 struct cYAML *seq_no, *prim_nid, *non_mr;
2829 num = yaml_copy_peer_nids(tree, &nids);
2833 seq_no = cYAML_get_object_item(tree, "seq_no");
2834 prim_nid = cYAML_get_object_item(tree, "primary nid");
2835 non_mr = cYAML_get_object_item(tree, "non_mr");
2837 rc = lustre_lnet_config_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
2839 (non_mr) ? false : true,
2840 (seq_no) ? seq_no->cy_valueint : -1,
2843 yaml_free_string_array(nids, num);
2847 static int handle_yaml_del_peer(struct cYAML *tree, struct cYAML **show_rc,
2848 struct cYAML **err_rc)
2852 struct cYAML *seq_no, *prim_nid;
2854 num = yaml_copy_peer_nids(tree, &nids);
2858 seq_no = cYAML_get_object_item(tree, "seq_no");
2859 prim_nid = cYAML_get_object_item(tree, "primary nid");
2861 rc = lustre_lnet_del_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
2863 (seq_no) ? seq_no->cy_valueint : -1,
2866 yaml_free_string_array(nids, num);
2870 static int handle_yaml_config_buffers(struct cYAML *tree,
2871 struct cYAML **show_rc,
2872 struct cYAML **err_rc)
2875 struct cYAML *tiny, *small, *large, *seq_no;
2877 tiny = cYAML_get_object_item(tree, "tiny");
2878 small = cYAML_get_object_item(tree, "small");
2879 large = cYAML_get_object_item(tree, "large");
2880 seq_no = cYAML_get_object_item(tree, "seq_no");
2882 rc = lustre_lnet_config_buffers((tiny) ? tiny->cy_valueint : -1,
2883 (small) ? small->cy_valueint : -1,
2884 (large) ? large->cy_valueint : -1,
2885 (seq_no) ? seq_no->cy_valueint : -1,
2891 static int handle_yaml_config_routing(struct cYAML *tree,
2892 struct cYAML **show_rc,
2893 struct cYAML **err_rc)
2895 int rc = LUSTRE_CFG_RC_NO_ERR;
2896 struct cYAML *seq_no, *enable;
2898 seq_no = cYAML_get_object_item(tree, "seq_no");
2899 enable = cYAML_get_object_item(tree, "enable");
2902 rc = lustre_lnet_enable_routing(enable->cy_valueint,
2904 seq_no->cy_valueint : -1,
2911 static int handle_yaml_del_route(struct cYAML *tree, struct cYAML **show_rc,
2912 struct cYAML **err_rc)
2916 struct cYAML *seq_no;
2918 net = cYAML_get_object_item(tree, "net");
2919 gw = cYAML_get_object_item(tree, "gateway");
2920 seq_no = cYAML_get_object_item(tree, "seq_no");
2922 return lustre_lnet_del_route((net) ? net->cy_valuestring : NULL,
2923 (gw) ? gw->cy_valuestring : NULL,
2924 (seq_no) ? seq_no->cy_valueint : -1,
2928 static int handle_yaml_del_routing(struct cYAML *tree, struct cYAML **show_rc,
2929 struct cYAML **err_rc)
2931 struct cYAML *seq_no;
2933 seq_no = cYAML_get_object_item(tree, "seq_no");
2935 return lustre_lnet_enable_routing(0, (seq_no) ?
2936 seq_no->cy_valueint : -1,
2940 static int handle_yaml_show_route(struct cYAML *tree, struct cYAML **show_rc,
2941 struct cYAML **err_rc)
2947 struct cYAML *detail;
2948 struct cYAML *seq_no;
2950 net = cYAML_get_object_item(tree, "net");
2951 gw = cYAML_get_object_item(tree, "gateway");
2952 hop = cYAML_get_object_item(tree, "hop");
2953 prio = cYAML_get_object_item(tree, "priority");
2954 detail = cYAML_get_object_item(tree, "detail");
2955 seq_no = cYAML_get_object_item(tree, "seq_no");
2957 return lustre_lnet_show_route((net) ? net->cy_valuestring : NULL,
2958 (gw) ? gw->cy_valuestring : NULL,
2959 (hop) ? hop->cy_valueint : -1,
2960 (prio) ? prio->cy_valueint : -1,
2961 (detail) ? detail->cy_valueint : 0,
2962 (seq_no) ? seq_no->cy_valueint : -1,
2967 static int handle_yaml_show_net(struct cYAML *tree, struct cYAML **show_rc,
2968 struct cYAML **err_rc)
2970 struct cYAML *net, *detail, *seq_no;
2972 net = cYAML_get_object_item(tree, "net");
2973 detail = cYAML_get_object_item(tree, "detail");
2974 seq_no = cYAML_get_object_item(tree, "seq_no");
2976 return lustre_lnet_show_net((net) ? net->cy_valuestring : NULL,
2977 (detail) ? detail->cy_valueint : 0,
2978 (seq_no) ? seq_no->cy_valueint : -1,
2983 static int handle_yaml_show_routing(struct cYAML *tree, struct cYAML **show_rc,
2984 struct cYAML **err_rc)
2986 struct cYAML *seq_no;
2988 seq_no = cYAML_get_object_item(tree, "seq_no");
2990 return lustre_lnet_show_routing((seq_no) ? seq_no->cy_valueint : -1,
2994 static int handle_yaml_show_peers(struct cYAML *tree, struct cYAML **show_rc,
2995 struct cYAML **err_rc)
2997 struct cYAML *seq_no, *nid, *detail;
2999 seq_no = cYAML_get_object_item(tree, "seq_no");
3000 detail = cYAML_get_object_item(tree, "detail");
3001 nid = cYAML_get_object_item(tree, "nid");
3003 return lustre_lnet_show_peer((nid) ? nid->cy_valuestring : NULL,
3004 (detail) ? detail->cy_valueint : 0,
3005 (seq_no) ? seq_no->cy_valueint : -1,
3009 static int handle_yaml_show_stats(struct cYAML *tree, struct cYAML **show_rc,
3010 struct cYAML **err_rc)
3012 struct cYAML *seq_no;
3014 seq_no = cYAML_get_object_item(tree, "seq_no");
3016 return lustre_lnet_show_stats((seq_no) ? seq_no->cy_valueint : -1,
3020 static int handle_yaml_config_numa(struct cYAML *tree, struct cYAML **show_rc,
3021 struct cYAML **err_rc)
3023 struct cYAML *seq_no, *range;
3025 seq_no = cYAML_get_object_item(tree, "seq_no");
3026 range = cYAML_get_object_item(tree, "range");
3028 return lustre_lnet_config_numa_range(range ? range->cy_valueint : -1,
3029 seq_no ? seq_no->cy_valueint : -1,
3033 static int handle_yaml_del_numa(struct cYAML *tree, struct cYAML **show_rc,
3034 struct cYAML **err_rc)
3036 struct cYAML *seq_no;
3038 seq_no = cYAML_get_object_item(tree, "seq_no");
3040 return lustre_lnet_config_numa_range(0, seq_no ? seq_no->cy_valueint : -1,
3044 static int handle_yaml_show_numa(struct cYAML *tree, struct cYAML **show_rc,
3045 struct cYAML **err_rc)
3047 struct cYAML *seq_no;
3049 seq_no = cYAML_get_object_item(tree, "seq_no");
3051 return lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint : -1,
3055 struct lookup_cmd_hdlr_tbl {
3060 static struct lookup_cmd_hdlr_tbl lookup_config_tbl[] = {
3061 {"route", handle_yaml_config_route},
3062 {"net", handle_yaml_config_ni},
3063 {"ip2nets", handle_yaml_config_ip2nets},
3064 {"peer", handle_yaml_config_peer},
3065 {"routing", handle_yaml_config_routing},
3066 {"buffers", handle_yaml_config_buffers},
3067 {"numa", handle_yaml_config_numa},
3071 static struct lookup_cmd_hdlr_tbl lookup_del_tbl[] = {
3072 {"route", handle_yaml_del_route},
3073 {"net", handle_yaml_del_ni},
3074 {"peer", handle_yaml_del_peer},
3075 {"routing", handle_yaml_del_routing},
3076 {"numa", handle_yaml_del_numa},
3080 static struct lookup_cmd_hdlr_tbl lookup_show_tbl[] = {
3081 {"route", handle_yaml_show_route},
3082 {"net", handle_yaml_show_net},
3083 {"buffers", handle_yaml_show_routing},
3084 {"routing", handle_yaml_show_routing},
3085 {"peer", handle_yaml_show_peers},
3086 {"statistics", handle_yaml_show_stats},
3087 {"numa", handle_yaml_show_numa},
3091 static cmd_handler_t lookup_fn(char *key,
3092 struct lookup_cmd_hdlr_tbl *tbl)
3098 for (i = 0; tbl[i].name != NULL; i++) {
3099 if (strncmp(key, tbl[i].name, strlen(tbl[i].name)) == 0)
3106 static int lustre_yaml_cb_helper(char *f, struct lookup_cmd_hdlr_tbl *table,
3107 struct cYAML **show_rc, struct cYAML **err_rc)
3109 struct cYAML *tree, *item = NULL, *head, *child;
3111 char err_str[LNET_MAX_STR_LEN];
3112 int rc = LUSTRE_CFG_RC_NO_ERR, return_rc = LUSTRE_CFG_RC_NO_ERR;
3114 tree = cYAML_build_tree(f, NULL, 0, err_rc, false);
3116 return LUSTRE_CFG_RC_BAD_PARAM;
3118 child = tree->cy_child;
3119 while (child != NULL) {
3120 cb = lookup_fn(child->cy_string, table);
3122 snprintf(err_str, sizeof(err_str),
3123 "\"call back for '%s' not found\"",
3125 cYAML_build_error(LUSTRE_CFG_RC_BAD_PARAM, -1,
3126 "yaml", "helper", err_str, err_rc);
3130 if (cYAML_is_sequence(child)) {
3131 while ((head = cYAML_get_next_seq_item(child, &item))
3133 rc = cb(head, show_rc, err_rc);
3134 if (rc != LUSTRE_CFG_RC_NO_ERR)
3138 rc = cb(child, show_rc, err_rc);
3139 if (rc != LUSTRE_CFG_RC_NO_ERR)
3143 child = child->cy_next;
3147 cYAML_free_tree(tree);
3152 int lustre_yaml_config(char *f, struct cYAML **err_rc)
3154 return lustre_yaml_cb_helper(f, lookup_config_tbl,
3158 int lustre_yaml_del(char *f, struct cYAML **err_rc)
3160 return lustre_yaml_cb_helper(f, lookup_del_tbl,
3164 int lustre_yaml_show(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
3166 return lustre_yaml_cb_helper(f, lookup_show_tbl,
3170 int lustre_lnet_send_dbg_task(enum lnet_dbg_task dbg_task,
3171 struct lnet_dbg_task_info *dbg_info,
3172 struct cYAML **show_rc,
3173 struct cYAML **err_rc)
3175 struct lnet_ioctl_dbg *dbg;
3176 struct lnet_dbg_task_info *info;
3177 int rc = LUSTRE_CFG_RC_NO_ERR;
3178 char err_str[LNET_MAX_STR_LEN];
3180 snprintf(err_str, sizeof(err_str), "\"success\"");
3182 dbg = calloc(1, sizeof(*dbg) + sizeof(*info));
3184 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3185 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3189 info = (struct lnet_dbg_task_info *)dbg->dbg_bulk;
3191 LIBCFS_IOC_INIT_V2(*dbg, dbg_hdr);
3193 dbg->dbg_task = dbg_task;
3195 memcpy(info, dbg_info, sizeof(*info));
3197 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DBG, dbg);
3202 "\"debug task failed %s\"", strerror(errno));
3207 cYAML_build_error(rc, -1, DBG_CMD,
3208 "debug", err_str, err_rc);