4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of the
9 * License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 * Copyright (c) 2014, 2016, Intel Corporation.
24 * Amir Shehata <amir.shehata@intel.com>
29 * 1. APIs that take the actual parameters expanded. This is for other
30 * entities that would like to link against the library and call the APIs
31 * directly without having to form an intermediate representation.
32 * 2. APIs that take a YAML file and parses out the information there and
33 * calls the APIs mentioned in 1
44 #include <sys/ioctl.h>
46 #include <libcfs/util/ioctl.h>
47 #include <linux/lnet/lnetctl.h>
49 #include <sys/types.h>
53 #include "liblnetconfig.h"
56 #define CONFIG_CMD "configure"
57 #define UNCONFIG_CMD "unconfigure"
60 #define SHOW_CMD "show"
63 #define modparam_path "/sys/module/lnet/parameters/"
66 * lustre_lnet_ip_range_descr
67 * Describes an IP range.
68 * Each octect is an expression
70 struct lustre_lnet_ip_range_descr {
71 struct list_head ipr_entry;
72 struct list_head ipr_expr;
77 * Describes an ip2nets rule. This can be on a list of rules.
79 struct lustre_lnet_ip2nets {
80 struct lnet_dlc_network_descr ip2nets_net;
81 struct list_head ip2nets_ip_ranges;
84 int open_sysfs_file(const char *path, const char *attr, const int mode)
87 char filename[LNET_MAX_STR_LEN];
89 if (strlen(path) + strlen(attr) >= LNET_MAX_STR_LEN)
92 snprintf(filename, sizeof(filename), "%s%s",
95 fd = open(filename, mode);
100 static int read_sysfs_file(const char *path, const char *attr,
101 void *val, const size_t size, const int nelem)
104 int rc = LUSTRE_CFG_RC_GENERIC_ERR;
106 fd = open_sysfs_file(path, attr, O_RDONLY);
108 return LUSTRE_CFG_RC_NO_MATCH;
110 if (read(fd, val, size * nelem) == -1)
113 rc = LUSTRE_CFG_RC_NO_ERR;
120 static int write_sysfs_file(const char *path, const char *attr,
121 void *val, const size_t size, const int nelem)
124 int rc = LUSTRE_CFG_RC_GENERIC_ERR;
126 fd = open_sysfs_file(path, attr, O_WRONLY | O_TRUNC);
128 return LUSTRE_CFG_RC_NO_MATCH;
130 if (write(fd, val, size * nelem) == -1)
133 rc = LUSTRE_CFG_RC_NO_ERR;
142 * frees the memory allocated for an intf descriptor.
144 void free_intf_descr(struct lnet_dlc_intf_descr *intf_descr)
149 if (intf_descr->cpt_expr != NULL)
150 cfs_expr_list_free(intf_descr->cpt_expr);
155 * lustre_lnet_add_ip_range
157 * given a string of the format:
158 * <expr.expr.expr.expr> parse each expr into
159 * a lustre_lnet_ip_range_descr structure and insert on the list.
161 * This function is called from
162 * YAML on each ip-range.
163 * As a result of lnetctl command
164 * When building a NID or P2P selection rules
166 int lustre_lnet_add_ip_range(struct list_head *list, char *str_ip_range)
168 struct lustre_lnet_ip_range_descr *ip_range;
171 ip_range = calloc(1, sizeof(*ip_range));
172 if (ip_range == NULL)
173 return LUSTRE_CFG_RC_OUT_OF_MEM;
175 INIT_LIST_HEAD(&ip_range->ipr_entry);
176 INIT_LIST_HEAD(&ip_range->ipr_expr);
178 rc = cfs_ip_addr_parse(str_ip_range, strlen(str_ip_range),
179 &ip_range->ipr_expr);
181 return LUSTRE_CFG_RC_BAD_PARAM;
183 list_add_tail(&ip_range->ipr_entry, list);
185 return LUSTRE_CFG_RC_NO_ERR;
188 int lustre_lnet_add_intf_descr(struct list_head *list, char *intf, int len)
190 char *open_sq_bracket = NULL, *close_sq_bracket = NULL,
192 struct lnet_dlc_intf_descr *intf_descr = NULL;
194 char intf_string[LNET_MAX_STR_LEN];
196 if (len >= LNET_MAX_STR_LEN)
197 return LUSTRE_CFG_RC_BAD_PARAM;
199 strncpy(intf_string, intf, len);
200 intf_string[len] = '\0';
202 intf_descr = calloc(1, sizeof(*intf_descr));
203 if (intf_descr == NULL)
204 return LUSTRE_CFG_RC_OUT_OF_MEM;
206 INIT_LIST_HEAD(&intf_descr->intf_on_network);
208 intf_name = intf_string;
209 open_sq_bracket = strchr(intf_string, '[');
210 if (open_sq_bracket != NULL) {
211 close_sq_bracket = strchr(intf_string, ']');
212 if (close_sq_bracket == NULL) {
214 return LUSTRE_CFG_RC_BAD_PARAM;
216 rc = cfs_expr_list_parse(open_sq_bracket,
217 strlen(open_sq_bracket), 0, UINT_MAX,
218 &intf_descr->cpt_expr);
221 return LUSTRE_CFG_RC_BAD_PARAM;
223 strncpy(intf_descr->intf_name, intf_name,
224 open_sq_bracket - intf_name);
225 intf_descr->intf_name[open_sq_bracket - intf_name] = '\0';
227 strcpy(intf_descr->intf_name, intf_name);
228 intf_descr->cpt_expr = NULL;
231 list_add_tail(&intf_descr->intf_on_network, list);
233 return LUSTRE_CFG_RC_NO_ERR;
236 void lustre_lnet_init_nw_descr(struct lnet_dlc_network_descr *nw_descr)
238 if (nw_descr != NULL) {
239 INIT_LIST_HEAD(&nw_descr->network_on_rule);
240 INIT_LIST_HEAD(&nw_descr->nw_intflist);
244 int lustre_lnet_parse_nids(char *nids, char **array, int size,
248 char *comma = nids, *cur, *entry;
250 int i, len, start = 0, finish = 0;
252 if (nids == NULL || strlen(nids) == 0)
255 /* count the number or new nids, by counting the number of commas */
257 comma = strchr(comma, ',');
267 * if the array is not NULL allocate a large enough array to house
268 * the old and new entries
270 new_array = calloc(sizeof(char*),
271 (size > 0) ? size + num_nids : num_nids);
276 /* parse our the new nids and add them to the tail of the array */
279 start = (size > 0) ? size: 0;
280 finish = (size > 0) ? size + num_nids : num_nids;
281 for (i = start; i < finish; i++) {
282 comma = strchr(comma, ',');
285 * the length of the string to be parsed out is
286 * from cur to end of string. So it's good enough
289 len = strlen(cur) + 1;
291 /* length of the string is comma - cur */
292 len = (comma - cur) + 1;
294 entry = calloc(1, len);
296 finish = i > 0 ? i - 1: 0;
299 strncpy(entry, cur, len - 1);
301 new_array[i] = entry;
308 /* add the old entries in the array and delete the old array*/
309 for (i = 0; i < size; i++)
310 new_array[i] = array[i];
315 *out_array = new_array;
320 for (i = start; i < finish; i++)
330 * <intf>[<expr>], <intf>[<expr>],..
332 int lustre_lnet_parse_interfaces(char *intf_str,
333 struct lnet_dlc_network_descr *nw_descr)
338 char *cur = intf_str, *next = NULL;
339 char *end = intf_str + strlen(intf_str);
341 struct lnet_dlc_intf_descr *intf_descr, *tmp;
343 if (nw_descr == NULL)
344 return LUSTRE_CFG_RC_BAD_PARAM;
347 open_square = strchr(cur, '[');
348 if (open_square != NULL) {
349 close_square = strchr(cur, ']');
350 if (close_square == NULL) {
351 rc = LUSTRE_CFG_RC_BAD_PARAM;
355 comma = strchr(cur, ',');
356 if (comma != NULL && comma > close_square) {
358 len = next - close_square;
364 comma = strchr(cur, ',');
374 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist, cur, len);
375 if (rc != LUSTRE_CFG_RC_NO_ERR)
381 return LUSTRE_CFG_RC_NO_ERR;
384 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
386 list_del(&intf_descr->intf_on_network);
387 free_intf_descr(intf_descr);
393 int lustre_lnet_config_lib_init(void)
395 return register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH,
396 LNET_DEV_MAJOR, LNET_DEV_MINOR);
399 void lustre_lnet_config_lib_uninit(void)
401 unregister_ioc_dev(LNET_DEV_ID);
404 int lustre_lnet_config_ni_system(bool up, bool load_ni_from_mod,
405 int seq_no, struct cYAML **err_rc)
407 struct libcfs_ioctl_data data;
410 char err_str[LNET_MAX_STR_LEN];
412 snprintf(err_str, sizeof(err_str), "\"Success\"");
414 LIBCFS_IOC_INIT(data);
416 /* Reverse logic is used here in order not to change
417 * the lctl utility */
418 data.ioc_flags = load_ni_from_mod ? 0 : 1;
420 opc = up ? IOC_LIBCFS_CONFIGURE : IOC_LIBCFS_UNCONFIGURE;
422 rc = l_ioctl(LNET_DEV_ID, opc, &data);
426 "\"LNet %s error: %s\"", (up) ? "configure" :
427 "unconfigure", strerror(errno));
431 cYAML_build_error(rc, seq_no, (up) ? CONFIG_CMD : UNCONFIG_CMD,
432 "lnet", err_str, err_rc);
437 static lnet_nid_t *allocate_create_nid_array(char **nids, __u32 num_nids,
440 lnet_nid_t *array = NULL;
443 if (!nids || num_nids == 0) {
444 snprintf(err_str, LNET_MAX_STR_LEN, "no NIDs to add");
448 array = calloc(sizeof(*array) * num_nids, 1);
450 snprintf(err_str, LNET_MAX_STR_LEN, "out of memory");
454 for (i = 0; i < num_nids; i++) {
455 array[i] = libcfs_str2nid(nids[i]);
456 if (array[i] == LNET_NID_ANY) {
458 snprintf(err_str, LNET_MAX_STR_LEN,
468 static int dispatch_peer_ni_cmd(lnet_nid_t pnid, lnet_nid_t nid, __u32 cmd,
469 struct lnet_ioctl_peer_cfg *data,
470 char *err_str, char *cmd_str)
474 data->prcfg_prim_nid = pnid;
475 data->prcfg_cfg_nid = nid;
477 rc = l_ioctl(LNET_DEV_ID, cmd, data);
482 "\"cannot %s peer ni: %s\"",
483 (cmd_str) ? cmd_str : "add", strerror(errno));
489 int lustre_lnet_config_peer_nid(char *pnid, char **nid, int num_nids,
490 bool mr, int seq_no, struct cYAML **err_rc)
492 struct lnet_ioctl_peer_cfg data;
493 lnet_nid_t prim_nid = LNET_NID_ANY;
494 int rc = LUSTRE_CFG_RC_NO_ERR;
496 bool nid0_used = false;
497 char err_str[LNET_MAX_STR_LEN] = {0};
498 lnet_nid_t *nids = allocate_create_nid_array(nid, num_nids, err_str);
501 prim_nid = libcfs_str2nid(pnid);
502 if (prim_nid == LNET_NID_ANY) {
503 snprintf(err_str, sizeof(err_str),
506 rc = LUSTRE_CFG_RC_MISSING_PARAM;
509 } else if (!nids || nids[0] == LNET_NID_ANY) {
510 snprintf(err_str, sizeof(err_str),
511 "no NIDs provided for configuration");
512 rc = LUSTRE_CFG_RC_MISSING_PARAM;
515 prim_nid = LNET_NID_ANY;
518 snprintf(err_str, sizeof(err_str), "\"Success\"");
520 LIBCFS_IOC_INIT_V2(data, prcfg_hdr);
524 * if prim_nid is not specified use the first nid in the list of
525 * nids provided as the prim_nid. NOTE: on entering 'if' we must
526 * have at least 1 NID
528 if (prim_nid == LNET_NID_ANY) {
533 /* Create the prim_nid first */
534 rc = dispatch_peer_ni_cmd(prim_nid, LNET_NID_ANY,
535 IOC_LIBCFS_ADD_PEER_NI,
536 &data, err_str, "add");
541 /* add the rest of the nids to the key nid if any are available */
542 for (idx = nid0_used ? 1 : 0 ; nids && idx < num_nids; idx++) {
544 * If prim_nid is not provided then the first nid in the
545 * list becomes the prim_nid. First time round the loop use
546 * LNET_NID_ANY for the first parameter, then use nid[0]
547 * as the key nid after wards
549 rc = dispatch_peer_ni_cmd(prim_nid, nids[idx],
550 IOC_LIBCFS_ADD_PEER_NI, &data,
560 cYAML_build_error(rc, seq_no, ADD_CMD, "peer_ni", err_str, err_rc);
564 int lustre_lnet_del_peer_nid(char *pnid, char **nid, int num_nids,
565 int seq_no, struct cYAML **err_rc)
567 struct lnet_ioctl_peer_cfg data;
569 int rc = LUSTRE_CFG_RC_NO_ERR;
571 char err_str[LNET_MAX_STR_LEN] = {0};
572 lnet_nid_t *nids = allocate_create_nid_array(nid, num_nids, err_str);
575 snprintf(err_str, sizeof(err_str),
576 "\"Primary nid is not provided\"");
577 rc = LUSTRE_CFG_RC_MISSING_PARAM;
580 prim_nid = libcfs_str2nid(pnid);
581 if (prim_nid == LNET_NID_ANY) {
582 rc = LUSTRE_CFG_RC_BAD_PARAM;
583 snprintf(err_str, sizeof(err_str),
590 snprintf(err_str, sizeof(err_str), "\"Success\"");
592 LIBCFS_IOC_INIT_V2(data, prcfg_hdr);
593 if (!nids || nids[0] == LNET_NID_ANY) {
594 rc = dispatch_peer_ni_cmd(prim_nid, LNET_NID_ANY,
595 IOC_LIBCFS_DEL_PEER_NI,
596 &data, err_str, "del");
600 for (idx = 0; nids && idx < num_nids; idx++) {
601 rc = dispatch_peer_ni_cmd(prim_nid, nids[idx],
602 IOC_LIBCFS_DEL_PEER_NI, &data,
612 cYAML_build_error(rc, seq_no, DEL_CMD, "peer_ni", err_str, err_rc);
616 int lustre_lnet_config_route(char *nw, char *gw, int hops, int prio,
617 int seq_no, struct cYAML **err_rc)
619 struct lnet_ioctl_config_data data;
620 lnet_nid_t gateway_nid;
621 int rc = LUSTRE_CFG_RC_NO_ERR;
622 __u32 net = LNET_NIDNET(LNET_NID_ANY);
623 char err_str[LNET_MAX_STR_LEN];
625 snprintf(err_str, sizeof(err_str), "\"Success\"");
627 if (nw == NULL || gw == NULL) {
630 "\"missing mandatory parameter(s): '%s'\"",
631 (nw == NULL && gw == NULL) ? "network, gateway" :
632 (nw == NULL) ? "network" : "gateway");
633 rc = LUSTRE_CFG_RC_MISSING_PARAM;
637 net = libcfs_str2net(nw);
638 if (net == LNET_NIDNET(LNET_NID_ANY)) {
641 "\"cannot parse net %s\"", nw);
642 rc = LUSTRE_CFG_RC_BAD_PARAM;
646 gateway_nid = libcfs_str2nid(gw);
647 if (gateway_nid == LNET_NID_ANY) {
650 "\"cannot parse gateway NID '%s'\"", gw);
651 rc = LUSTRE_CFG_RC_BAD_PARAM;
656 /* hops is undefined */
657 hops = LNET_UNDEFINED_HOPS;
658 } else if (hops < 1 || hops > 255) {
661 "\"invalid hop count %d, must be between 1 and 255\"",
663 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
669 } else if (prio < 0) {
672 "\"invalid priority %d, must be greater than 0\"",
674 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
678 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
680 data.cfg_config_u.cfg_route.rtr_hop = hops;
681 data.cfg_config_u.cfg_route.rtr_priority = prio;
682 data.cfg_nid = gateway_nid;
684 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
689 "\"cannot add route: %s\"", strerror(errno));
694 cYAML_build_error(rc, seq_no, ADD_CMD, "route", err_str, err_rc);
699 int lustre_lnet_del_route(char *nw, char *gw,
700 int seq_no, struct cYAML **err_rc)
702 struct lnet_ioctl_config_data data;
703 lnet_nid_t gateway_nid;
704 int rc = LUSTRE_CFG_RC_NO_ERR;
705 __u32 net = LNET_NIDNET(LNET_NID_ANY);
706 char err_str[LNET_MAX_STR_LEN];
708 snprintf(err_str, sizeof(err_str), "\"Success\"");
710 if (nw == NULL || gw == NULL) {
713 "\"missing mandatory parameter(s): '%s'\"",
714 (nw == NULL && gw == NULL) ? "network, gateway" :
715 (nw == NULL) ? "network" : "gateway");
716 rc = LUSTRE_CFG_RC_MISSING_PARAM;
720 net = libcfs_str2net(nw);
721 if (net == LNET_NIDNET(LNET_NID_ANY)) {
724 "\"cannot parse net '%s'\"", nw);
725 rc = LUSTRE_CFG_RC_BAD_PARAM;
729 gateway_nid = libcfs_str2nid(gw);
730 if (gateway_nid == LNET_NID_ANY) {
733 "\"cannot parse gateway NID '%s'\"", gw);
734 rc = LUSTRE_CFG_RC_BAD_PARAM;
738 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
740 data.cfg_nid = gateway_nid;
742 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
747 "\"cannot delete route: %s\"", strerror(errno));
752 cYAML_build_error(rc, seq_no, DEL_CMD, "route", err_str, err_rc);
757 int lustre_lnet_show_route(char *nw, char *gw, int hops, int prio, int detail,
758 int seq_no, struct cYAML **show_rc,
759 struct cYAML **err_rc)
761 struct lnet_ioctl_config_data data;
762 lnet_nid_t gateway_nid;
763 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
765 __u32 net = LNET_NIDNET(LNET_NID_ANY);
767 struct cYAML *root = NULL, *route = NULL, *item = NULL;
768 struct cYAML *first_seq = NULL;
769 char err_str[LNET_MAX_STR_LEN];
772 snprintf(err_str, sizeof(err_str),
773 "\"out of memory\"");
776 net = libcfs_str2net(nw);
777 if (net == LNET_NIDNET(LNET_NID_ANY)) {
780 "\"cannot parse net '%s'\"", nw);
781 rc = LUSTRE_CFG_RC_BAD_PARAM;
786 /* show all routes without filtering on net */
787 net = LNET_NIDNET(LNET_NID_ANY);
791 gateway_nid = libcfs_str2nid(gw);
792 if (gateway_nid == LNET_NID_ANY) {
795 "\"cannot parse gateway NID '%s'\"", gw);
796 rc = LUSTRE_CFG_RC_BAD_PARAM;
800 /* show all routes with out filtering on gateway */
801 gateway_nid = LNET_NID_ANY;
803 if ((hops < 1 && hops != -1) || hops > 255) {
806 "\"invalid hop count %d, must be between 0 and 256\"",
808 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
812 /* create struct cYAML root object */
813 root = cYAML_create_object(NULL, NULL);
817 route = cYAML_create_seq(root, "route");
822 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
825 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
831 /* filter on provided data */
832 if (net != LNET_NIDNET(LNET_NID_ANY) &&
836 if (gateway_nid != LNET_NID_ANY &&
837 gateway_nid != data.cfg_nid)
841 hops != data.cfg_config_u.cfg_route.rtr_hop)
845 prio != data.cfg_config_u.cfg_route.rtr_priority)
848 /* default rc to -1 incase we hit the goto */
852 item = cYAML_create_seq_item(route);
856 if (first_seq == NULL)
859 if (cYAML_create_string(item, "net",
860 libcfs_net2str(data.cfg_net)) == NULL)
863 if (cYAML_create_string(item, "gateway",
864 libcfs_nid2str(data.cfg_nid)) == NULL)
868 if (cYAML_create_number(item, "hop",
869 (int) data.cfg_config_u.
870 cfg_route.rtr_hop) ==
874 if (cYAML_create_number(item, "priority",
876 cfg_route.rtr_priority) == NULL)
879 if (cYAML_create_string(item, "state",
880 data.cfg_config_u.cfg_route.
882 "up" : "down") == NULL)
887 /* print output iff show_rc is not provided */
889 cYAML_print_tree(root);
891 if (l_errno != ENOENT) {
894 "\"cannot get routes: %s\"",
899 rc = LUSTRE_CFG_RC_NO_ERR;
901 snprintf(err_str, sizeof(err_str), "\"success\"");
903 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
904 cYAML_free_tree(root);
905 } else if (show_rc != NULL && *show_rc != NULL) {
906 struct cYAML *show_node;
907 /* find the route node, if one doesn't exist then
908 * insert one. Otherwise add to the one there
910 show_node = cYAML_get_object_item(*show_rc, "route");
911 if (show_node != NULL && cYAML_is_sequence(show_node)) {
912 cYAML_insert_child(show_node, first_seq);
915 } else if (show_node == NULL) {
916 cYAML_insert_sibling((*show_rc)->cy_child,
920 cYAML_free_tree(root);
926 cYAML_build_error(rc, seq_no, SHOW_CMD, "route", err_str, err_rc);
931 static int socket_intf_query(int request, char *intf,
937 if (strlen(intf) >= IFNAMSIZ || ifr == NULL)
938 return LUSTRE_CFG_RC_BAD_PARAM;
940 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
942 return LUSTRE_CFG_RC_BAD_PARAM;
944 strcpy(ifr->ifr_name, intf);
945 rc = ioctl(sockfd, request, ifr);
947 rc = LUSTRE_CFG_RC_BAD_PARAM;
955 * for each interface in the array of interfaces find the IP address of
956 * that interface, create its nid and add it to an array of NIDs.
957 * Stop if any of the interfaces is down
959 static int lustre_lnet_intf2nids(struct lnet_dlc_network_descr *nw,
960 lnet_nid_t **nids, __u32 *nnids)
962 int i = 0, count = 0, rc;
965 struct lnet_dlc_intf_descr *intf;
967 if (nw == NULL || nids == NULL)
968 return LUSTRE_CFG_RC_BAD_PARAM;
970 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network)
973 *nids = calloc(count, sizeof(lnet_nid_t));
975 return LUSTRE_CFG_RC_OUT_OF_MEM;
977 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network) {
978 memset(&ifr, 0, sizeof(ifr));
979 rc = socket_intf_query(SIOCGIFFLAGS, intf->intf_name, &ifr);
983 if ((ifr.ifr_flags & IFF_UP) == 0) {
984 rc = LUSTRE_CFG_RC_BAD_PARAM;
988 memset(&ifr, 0, sizeof(ifr));
989 rc = socket_intf_query(SIOCGIFADDR, intf->intf_name, &ifr);
993 ip = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
995 (*nids)[i] = LNET_MKNID(nw->nw_id, ip);
1010 * called repeatedly until a match or no more ip range
1012 * ip_range expression
1013 * interface list with all the interface names.
1014 * all the interfaces in the system.
1016 * try to match the ip_range expr to one of the interfaces' IPs in
1017 * the system. If we hit a patch for an interface. Check if that
1018 * interface name is in the list.
1020 * If there are more than one interface in the list, then make sure
1021 * that the IPs for all of these interfaces match the ip ranges
1024 * for each interface in intf_list
1025 * look up the intf name in ifa
1026 * if not there then no match
1027 * check ip obtained from ifa against a match to any of the
1029 * If no match, then fail
1031 * The result is that all the interfaces have to match.
1033 int lustre_lnet_match_ip_to_intf(struct ifaddrs *ifa,
1034 struct list_head *intf_list,
1035 struct list_head *ip_ranges)
1039 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1040 struct ifaddrs *ifaddr = ifa;
1041 struct lustre_lnet_ip_range_descr *ip_range;
1045 * if there are no explicit interfaces, and no ip ranges, then
1046 * configure the first tcp interface we encounter.
1048 if (list_empty(intf_list) && list_empty(ip_ranges)) {
1049 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1050 if (ifaddr->ifa_addr == NULL)
1053 if ((ifaddr->ifa_flags & IFF_UP) == 0)
1056 family = ifaddr->ifa_addr->sa_family;
1057 if (family == AF_INET &&
1058 strcmp(ifaddr->ifa_name, "lo") != 0) {
1059 rc = lustre_lnet_add_intf_descr
1060 (intf_list, ifaddr->ifa_name,
1061 strlen(ifaddr->ifa_name));
1063 if (rc != LUSTRE_CFG_RC_NO_ERR)
1066 return LUSTRE_CFG_RC_MATCH;
1069 return LUSTRE_CFG_RC_NO_MATCH;
1073 * First interface which matches an IP pattern will be used
1075 if (list_empty(intf_list)) {
1077 * no interfaces provided in the rule, but an ip range is
1078 * provided, so try and match an interface to the ip
1081 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1082 if (ifaddr->ifa_addr == NULL)
1085 if ((ifaddr->ifa_flags & IFF_UP) == 0)
1088 family = ifaddr->ifa_addr->sa_family;
1089 if (family == AF_INET) {
1090 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->
1093 list_for_each_entry(ip_range, ip_ranges,
1095 rc = cfs_ip_addr_match(bswap_32(ip),
1096 &ip_range->ipr_expr);
1100 rc = lustre_lnet_add_intf_descr
1101 (intf_list, ifaddr->ifa_name,
1102 strlen(ifaddr->ifa_name));
1104 if (rc != LUSTRE_CFG_RC_NO_ERR)
1110 if (!list_empty(intf_list))
1111 return LUSTRE_CFG_RC_MATCH;
1113 return LUSTRE_CFG_RC_NO_MATCH;
1117 * If an interface is explicitly specified the ip-range might or
1118 * might not be specified. if specified the interface needs to match the
1119 * ip-range. If no ip-range then the interfaces are
1120 * automatically matched if they are all up.
1121 * If > 1 interfaces all the interfaces must match for the NI to
1124 list_for_each_entry_safe(intf_descr, tmp, intf_list, intf_on_network) {
1125 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1126 if (ifaddr->ifa_addr == NULL)
1129 family = ifaddr->ifa_addr->sa_family;
1130 if (family == AF_INET &&
1131 strcmp(intf_descr->intf_name,
1132 ifaddr->ifa_name) == 0)
1136 if (ifaddr == NULL) {
1137 list_del(&intf_descr->intf_on_network);
1138 free_intf_descr(intf_descr);
1142 if ((ifaddr->ifa_flags & IFF_UP) == 0) {
1143 list_del(&intf_descr->intf_on_network);
1144 free_intf_descr(intf_descr);
1148 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr.s_addr;
1151 list_for_each_entry(ip_range, ip_ranges, ipr_entry) {
1152 rc = cfs_ip_addr_match(bswap_32(ip), &ip_range->ipr_expr);
1158 /* no match for this interface */
1159 list_del(&intf_descr->intf_on_network);
1160 free_intf_descr(intf_descr);
1164 return LUSTRE_CFG_RC_MATCH;
1167 int lustre_lnet_resolve_ip2nets_rule(struct lustre_lnet_ip2nets *ip2nets,
1168 lnet_nid_t **nids, __u32 *nnids)
1170 struct ifaddrs *ifa;
1171 int rc = LUSTRE_CFG_RC_NO_ERR;
1173 rc = getifaddrs(&ifa);
1177 rc = lustre_lnet_match_ip_to_intf(ifa,
1178 &ip2nets->ip2nets_net.nw_intflist,
1179 &ip2nets->ip2nets_ip_ranges);
1180 if (rc != LUSTRE_CFG_RC_MATCH) {
1185 rc = lustre_lnet_intf2nids(&ip2nets->ip2nets_net, nids, nnids);
1186 if (rc != LUSTRE_CFG_RC_NO_ERR) {
1197 lustre_lnet_ioctl_config_ni(struct list_head *intf_list,
1198 struct lnet_ioctl_config_lnd_tunables *tunables,
1199 struct cfs_expr_list *global_cpts,
1200 lnet_nid_t *nids, char *err_str)
1203 struct lnet_ioctl_config_ni *conf;
1204 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1205 int rc = LUSTRE_CFG_RC_NO_ERR, i = 0;
1208 struct lnet_dlc_intf_descr *intf_descr;
1210 struct cfs_expr_list *cpt_expr;
1212 list_for_each_entry(intf_descr, intf_list,
1214 if (tunables != NULL)
1215 len = sizeof(struct lnet_ioctl_config_ni) +
1216 sizeof(struct lnet_ioctl_config_lnd_tunables);
1218 len = sizeof(struct lnet_ioctl_config_ni);
1220 data = calloc(1, len);
1222 return LUSTRE_CFG_RC_OUT_OF_MEM;
1223 conf = (struct lnet_ioctl_config_ni*) data;
1224 if (tunables != NULL)
1225 tun = (struct lnet_ioctl_config_lnd_tunables*)
1228 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1229 conf->lic_cfg_hdr.ioc_len = len;
1230 conf->lic_nid = nids[i];
1231 strncpy(conf->lic_ni_intf[0], intf_descr->intf_name,
1234 if (intf_descr->cpt_expr != NULL)
1235 cpt_expr = intf_descr->cpt_expr;
1236 else if (global_cpts != NULL)
1237 cpt_expr = global_cpts;
1241 if (cpt_expr != NULL) {
1242 count = cfs_expr_list_values(cpt_expr,
1243 LNET_MAX_SHOW_NUM_CPT,
1246 memcpy(conf->lic_cpts, cpt_array,
1247 sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1256 conf->lic_ncpts = count;
1258 if (tunables != NULL)
1259 memcpy(tun, tunables, sizeof(*tunables));
1261 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1266 "\"cannot add network: %s\"", strerror(errno));
1274 return LUSTRE_CFG_RC_NO_ERR;
1278 lustre_lnet_config_ip2nets(struct lustre_lnet_ip2nets *ip2nets,
1279 struct lnet_ioctl_config_lnd_tunables *tunables,
1280 struct cfs_expr_list *global_cpts,
1281 int seq_no, struct cYAML **err_rc)
1283 lnet_nid_t *nids = NULL;
1286 char err_str[LNET_MAX_STR_LEN];
1288 snprintf(err_str, sizeof(err_str), "\"success\"");
1293 "\"incomplete ip2nets information\"");
1294 rc = LUSTRE_CFG_RC_BAD_PARAM;
1299 * call below function to resolve the rules into a list of nids.
1300 * The memory is allocated in that function then freed here when
1301 * it's no longer needed.
1303 rc = lustre_lnet_resolve_ip2nets_rule(ip2nets, &nids, &nnids);
1304 if (rc != LUSTRE_CFG_RC_NO_ERR && rc != LUSTRE_CFG_RC_MATCH) {
1307 "\"cannot resolve ip2nets rule\"");
1311 if (list_empty(&ip2nets->ip2nets_net.nw_intflist)) {
1312 snprintf(err_str, sizeof(err_str),
1313 "\"no interfaces match ip2nets rules\"");
1317 rc = lustre_lnet_ioctl_config_ni(&ip2nets->ip2nets_net.nw_intflist,
1318 tunables, global_cpts, nids,
1325 cYAML_build_error(rc, seq_no, ADD_CMD, "ip2nets", err_str, err_rc);
1329 int lustre_lnet_config_ni(struct lnet_dlc_network_descr *nw_descr,
1330 struct cfs_expr_list *global_cpts,
1332 struct lnet_ioctl_config_lnd_tunables *tunables,
1333 int seq_no, struct cYAML **err_rc)
1336 struct lnet_ioctl_config_ni *conf;
1337 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1338 char buf[LNET_MAX_STR_LEN];
1339 int rc = LUSTRE_CFG_RC_NO_ERR;
1340 char err_str[LNET_MAX_STR_LEN];
1341 lnet_nid_t *nids = NULL;
1345 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1348 snprintf(err_str, sizeof(err_str), "\"success\"");
1350 if (ip2net == NULL && nw_descr == NULL) {
1353 "\"mandatory parameters not specified.\"");
1354 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1358 if (ip2net != NULL && strlen(ip2net) >= sizeof(buf)) {
1361 "\"ip2net string too long %d\"",
1362 (int)strlen(ip2net));
1363 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1367 if (ip2net != NULL) {
1368 if (tunables != NULL)
1369 len = sizeof(struct lnet_ioctl_config_ni) +
1370 sizeof(struct lnet_ioctl_config_lnd_tunables);
1372 len = sizeof(struct lnet_ioctl_config_ni);
1373 data = calloc(1, len);
1375 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1378 conf = (struct lnet_ioctl_config_ni*) data;
1379 if (tunables != NULL)
1380 tun = (struct lnet_ioctl_config_lnd_tunables*)
1381 (data + sizeof(*conf));
1383 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1384 conf->lic_cfg_hdr.ioc_len = len;
1385 strncpy(conf->lic_legacy_ip2nets, ip2net,
1388 if (global_cpts != NULL) {
1389 count = cfs_expr_list_values(global_cpts,
1390 LNET_MAX_SHOW_NUM_CPT,
1393 memcpy(conf->lic_cpts, cpt_array,
1394 sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1403 conf->lic_ncpts = count;
1405 if (tunables != NULL)
1406 memcpy(tun, tunables, sizeof(*tunables));
1408 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1413 "\"cannot add network: %s\"", strerror(errno));
1420 if (LNET_NETTYP(nw_descr->nw_id) == LOLND) {
1421 rc = LUSTRE_CFG_RC_NO_ERR;
1425 if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1428 "\"cannot parse net '%s'\"",
1429 libcfs_net2str(nw_descr->nw_id));
1430 rc = LUSTRE_CFG_RC_BAD_PARAM;
1434 if (list_empty(&nw_descr->nw_intflist)) {
1437 "\"no interface name provided\"");
1438 rc = LUSTRE_CFG_RC_BAD_PARAM;
1442 rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids);
1444 snprintf(err_str, sizeof(err_str),
1445 "\"bad parameter\"");
1446 rc = LUSTRE_CFG_RC_BAD_PARAM;
1450 rc = lustre_lnet_ioctl_config_ni(&nw_descr->nw_intflist,
1451 tunables, global_cpts, nids,
1455 if (nw_descr != NULL) {
1456 list_for_each_entry_safe(intf_descr, tmp,
1457 &nw_descr->nw_intflist,
1459 list_del(&intf_descr->intf_on_network);
1460 free_intf_descr(intf_descr);
1464 cYAML_build_error(rc, seq_no, ADD_CMD, "net", err_str, err_rc);
1475 int lustre_lnet_del_ni(struct lnet_dlc_network_descr *nw_descr,
1476 int seq_no, struct cYAML **err_rc)
1478 struct lnet_ioctl_config_ni data;
1479 int rc = LUSTRE_CFG_RC_NO_ERR, i;
1480 char err_str[LNET_MAX_STR_LEN];
1481 lnet_nid_t *nids = NULL;
1483 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1485 snprintf(err_str, sizeof(err_str), "\"success\"");
1487 if (nw_descr == NULL) {
1490 "\"missing mandatory parameter\"");
1491 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1495 if (LNET_NETTYP(nw_descr->nw_id) == LOLND)
1496 return LUSTRE_CFG_RC_NO_ERR;
1498 if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1501 "\"cannot parse net '%s'\"",
1502 libcfs_net2str(nw_descr->nw_id));
1503 rc = LUSTRE_CFG_RC_BAD_PARAM;
1507 rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids);
1509 snprintf(err_str, sizeof(err_str),
1510 "\"bad parameter\"");
1511 rc = LUSTRE_CFG_RC_BAD_PARAM;
1516 * no interfaces just the nw_id is specified
1519 nids = calloc(1, sizeof(*nids));
1521 snprintf(err_str, sizeof(err_str),
1522 "\"out of memory\"");
1523 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1526 nids[0] = LNET_MKNID(nw_descr->nw_id, 0);
1530 for (i = 0; i < nnids; i++) {
1531 LIBCFS_IOC_INIT_V2(data, lic_cfg_hdr);
1532 data.lic_nid = nids[i];
1534 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_LOCAL_NI, &data);
1539 "\"cannot del network: %s\"", strerror(errno));
1543 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
1545 list_del(&intf_descr->intf_on_network);
1546 free_intf_descr(intf_descr);
1550 cYAML_build_error(rc, seq_no, DEL_CMD, "net", err_str, err_rc);
1558 int lustre_lnet_show_net(char *nw, int detail, int seq_no,
1559 struct cYAML **show_rc, struct cYAML **err_rc)
1562 struct lnet_ioctl_config_ni *ni_data;
1563 struct lnet_ioctl_config_lnd_tunables *lnd;
1564 struct lnet_ioctl_element_stats *stats;
1565 __u32 net = LNET_NIDNET(LNET_NID_ANY);
1566 __u32 prev_net = LNET_NIDNET(LNET_NID_ANY);
1567 int rc = LUSTRE_CFG_RC_OUT_OF_MEM, i, j;
1569 struct cYAML *root = NULL, *tunables = NULL,
1570 *net_node = NULL, *interfaces = NULL,
1571 *item = NULL, *first_seq = NULL,
1572 *tmp = NULL, *statistics = NULL;
1573 int str_buf_len = LNET_MAX_SHOW_NUM_CPT * 2;
1574 char str_buf[str_buf_len];
1576 char err_str[LNET_MAX_STR_LEN];
1577 bool exist = false, new_net = true;
1579 size_t buf_size = sizeof(*ni_data) + sizeof(*lnd) + sizeof(*stats);
1581 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1583 buf = calloc(1, buf_size);
1587 ni_data = (struct lnet_ioctl_config_ni *)buf;
1590 net = libcfs_str2net(nw);
1591 if (net == LNET_NIDNET(LNET_NID_ANY)) {
1594 "\"cannot parse net '%s'\"", nw);
1595 rc = LUSTRE_CFG_RC_BAD_PARAM;
1600 root = cYAML_create_object(NULL, NULL);
1604 net_node = cYAML_create_seq(root, "net");
1605 if (net_node == NULL)
1612 memset(buf, 0, buf_size);
1614 LIBCFS_IOC_INIT_V2(*ni_data, lic_cfg_hdr);
1616 * set the ioc_len to the proper value since INIT assumes
1619 ni_data->lic_cfg_hdr.ioc_len = buf_size;
1620 ni_data->lic_idx = i;
1622 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LOCAL_NI, ni_data);
1628 rc_net = LNET_NIDNET(ni_data->lic_nid);
1630 /* filter on provided data */
1631 if (net != LNET_NIDNET(LNET_NID_ANY) &&
1635 /* default rc to -1 in case we hit the goto */
1639 stats = (struct lnet_ioctl_element_stats *)ni_data->lic_bulk;
1640 lnd = (struct lnet_ioctl_config_lnd_tunables *)
1641 (ni_data->lic_bulk + sizeof(*stats));
1643 if (rc_net != prev_net) {
1650 if (!cYAML_create_string(net_node, "net type",
1651 libcfs_net2str(rc_net)))
1654 tmp = cYAML_create_seq(net_node, "local NI(s)");
1660 /* create the tree to be printed. */
1661 item = cYAML_create_seq_item(tmp);
1665 if (first_seq == NULL)
1668 if (cYAML_create_string(item, "nid",
1669 libcfs_nid2str(ni_data->lic_nid)) == NULL)
1672 if (cYAML_create_string(item,
1674 (ni_data->lic_status ==
1675 LNET_NI_STATUS_UP) ?
1676 "up" : "down") == NULL)
1679 /* don't add interfaces unless there is at least one
1681 if (strlen(ni_data->lic_ni_intf[0]) > 0) {
1682 interfaces = cYAML_create_object(item, "interfaces");
1683 if (interfaces == NULL)
1686 for (j = 0; j < LNET_INTERFACES_NUM; j++) {
1687 if (strlen(ni_data->lic_ni_intf[j]) > 0) {
1689 sizeof(str_buf), "%d", j);
1690 if (cYAML_create_string(interfaces,
1692 ni_data->lic_ni_intf[j]) ==
1702 statistics = cYAML_create_object(item, "statistics");
1703 if (statistics == NULL)
1706 if (cYAML_create_number(statistics, "send_count",
1707 stats->iel_send_count)
1711 if (cYAML_create_number(statistics, "recv_count",
1712 stats->iel_recv_count)
1716 if (cYAML_create_number(statistics, "drop_count",
1717 stats->iel_drop_count)
1721 tunables = cYAML_create_object(item, "tunables");
1725 rc = lustre_net_show_tunables(tunables, &lnd->lt_cmn);
1726 if (rc != LUSTRE_CFG_RC_NO_ERR)
1729 tunables = cYAML_create_object(item, "lnd tunables");
1730 if (tunables == NULL)
1733 rc = lustre_ni_show_tunables(tunables, LNET_NETTYP(rc_net),
1735 if (rc != LUSTRE_CFG_RC_NO_ERR)
1738 if (cYAML_create_number(item, "tcp bonding",
1739 ni_data->lic_tcp_bonding)
1743 if (cYAML_create_number(item, "dev cpt",
1744 ni_data->lic_dev_cpt) == NULL)
1747 /* out put the CPTs in the format: "[x,x,x,...]" */
1748 limit = str_buf + str_buf_len - 3;
1749 pos += snprintf(pos, limit - pos, "\"[");
1750 for (j = 0 ; ni_data->lic_ncpts >= 1 &&
1751 j < ni_data->lic_ncpts &&
1753 pos += snprintf(pos, limit - pos,
1754 "%d", ni_data->lic_cpts[j]);
1755 if ((j + 1) < ni_data->lic_ncpts)
1756 pos += snprintf(pos, limit - pos, ",");
1758 pos += snprintf(pos, 3, "]\"");
1760 if (ni_data->lic_ncpts >= 1 &&
1761 cYAML_create_string(item, "CPT",
1767 /* Print out the net information only if show_rc is not provided */
1768 if (show_rc == NULL)
1769 cYAML_print_tree(root);
1771 if (l_errno != ENOENT) {
1774 "\"cannot get networks: %s\"",
1779 rc = LUSTRE_CFG_RC_NO_ERR;
1781 snprintf(err_str, sizeof(err_str), "\"success\"");
1783 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
1784 cYAML_free_tree(root);
1785 } else if (show_rc != NULL && *show_rc != NULL) {
1786 struct cYAML *show_node;
1787 /* find the net node, if one doesn't exist
1788 * then insert one. Otherwise add to the one there
1790 show_node = cYAML_get_object_item(*show_rc, "net");
1791 if (show_node != NULL && cYAML_is_sequence(show_node)) {
1792 cYAML_insert_child(show_node, first_seq);
1795 } else if (show_node == NULL) {
1796 cYAML_insert_sibling((*show_rc)->cy_child,
1800 cYAML_free_tree(root);
1806 cYAML_build_error(rc, seq_no, SHOW_CMD, "net", err_str, err_rc);
1811 int lustre_lnet_enable_routing(int enable, int seq_no, struct cYAML **err_rc)
1813 struct lnet_ioctl_config_data data;
1814 int rc = LUSTRE_CFG_RC_NO_ERR;
1815 char err_str[LNET_MAX_STR_LEN];
1817 snprintf(err_str, sizeof(err_str), "\"success\"");
1819 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1820 data.cfg_config_u.cfg_buffers.buf_enable = (enable) ? 1 : 0;
1822 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIG_RTR, &data);
1827 "\"cannot %s routing %s\"",
1828 (enable) ? "enable" : "disable", strerror(errno));
1833 cYAML_build_error(rc, seq_no,
1834 (enable) ? ADD_CMD : DEL_CMD,
1835 "routing", err_str, err_rc);
1840 int lustre_lnet_config_max_intf(int max, int seq_no, struct cYAML **err_rc)
1842 int rc = LUSTRE_CFG_RC_NO_ERR;
1843 char err_str[LNET_MAX_STR_LEN];
1844 char val[LNET_MAX_STR_LEN];
1846 snprintf(err_str, sizeof(err_str), "\"success\"");
1848 snprintf(val, sizeof(val), "%d", max);
1850 rc = write_sysfs_file(modparam_path, "lnet_interfaces_max", val,
1851 1, strlen(val) + 1);
1853 snprintf(err_str, sizeof(err_str),
1854 "\"cannot configure max interfaces: %s\"",
1857 cYAML_build_error(rc, seq_no, ADD_CMD, "max_interfaces", err_str, err_rc);
1862 int lustre_lnet_config_discovery(int enable, int seq_no, struct cYAML **err_rc)
1864 int rc = LUSTRE_CFG_RC_NO_ERR;
1865 char err_str[LNET_MAX_STR_LEN];
1866 char val[LNET_MAX_STR_LEN];
1868 snprintf(err_str, sizeof(err_str), "\"success\"");
1870 snprintf(val, sizeof(val), "%u", (enable) ? 0 : 1);
1872 rc = write_sysfs_file(modparam_path, "lnet_peer_discovery_disabled", val,
1873 1, strlen(val) + 1);
1875 snprintf(err_str, sizeof(err_str),
1876 "\"cannot configure discovery: %s\"",
1879 cYAML_build_error(rc, seq_no, ADD_CMD, "discovery", err_str, err_rc);
1885 int lustre_lnet_config_numa_range(int range, int seq_no, struct cYAML **err_rc)
1887 struct lnet_ioctl_set_value data;
1888 int rc = LUSTRE_CFG_RC_NO_ERR;
1889 char err_str[LNET_MAX_STR_LEN];
1891 snprintf(err_str, sizeof(err_str), "\"success\"");
1896 "\"range must be >= 0\"");
1897 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1901 LIBCFS_IOC_INIT_V2(data, sv_hdr);
1902 data.sv_value = range;
1904 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_SET_NUMA_RANGE, &data);
1909 "\"cannot configure numa_range: %s\"", strerror(errno));
1914 cYAML_build_error(rc, seq_no, ADD_CMD, "numa_range", err_str, err_rc);
1919 int lustre_lnet_config_buffers(int tiny, int small, int large, int seq_no,
1920 struct cYAML **err_rc)
1922 struct lnet_ioctl_config_data data;
1923 int rc = LUSTRE_CFG_RC_NO_ERR;
1924 char err_str[LNET_MAX_STR_LEN];
1926 snprintf(err_str, sizeof(err_str), "\"success\"");
1928 /* -1 indicates to ignore changes to this field */
1929 if (tiny < -1 || small < -1 || large < -1) {
1932 "\"tiny, small and large must be >= 0\"");
1933 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1937 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1938 data.cfg_config_u.cfg_buffers.buf_tiny = tiny;
1939 data.cfg_config_u.cfg_buffers.buf_small = small;
1940 data.cfg_config_u.cfg_buffers.buf_large = large;
1942 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_BUF, &data);
1947 "\"cannot configure buffers: %s\"", strerror(errno));
1952 cYAML_build_error(rc, seq_no, ADD_CMD, "buf", err_str, err_rc);
1957 int lustre_lnet_show_routing(int seq_no, struct cYAML **show_rc,
1958 struct cYAML **err_rc)
1960 struct lnet_ioctl_config_data *data;
1961 struct lnet_ioctl_pool_cfg *pool_cfg = NULL;
1962 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1965 char *pools[LNET_NRBPOOLS] = {"tiny", "small", "large"};
1966 int buf_count[LNET_NRBPOOLS] = {0};
1967 struct cYAML *root = NULL, *pools_node = NULL,
1968 *type_node = NULL, *item = NULL, *cpt = NULL,
1969 *first_seq = NULL, *buffers = NULL;
1971 char err_str[LNET_MAX_STR_LEN];
1972 char node_name[LNET_MAX_STR_LEN];
1975 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1977 buf = calloc(1, sizeof(*data) + sizeof(*pool_cfg));
1981 data = (struct lnet_ioctl_config_data *)buf;
1983 root = cYAML_create_object(NULL, NULL);
1987 pools_node = cYAML_create_seq(root, "routing");
1988 if (pools_node == NULL)
1992 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
1993 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
1994 sizeof(struct lnet_ioctl_pool_cfg);
1995 data->cfg_count = i;
1997 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_BUF, data);
2005 pool_cfg = (struct lnet_ioctl_pool_cfg *)data->cfg_bulk;
2007 snprintf(node_name, sizeof(node_name), "cpt[%d]", i);
2008 item = cYAML_create_seq_item(pools_node);
2012 if (first_seq == NULL)
2015 cpt = cYAML_create_object(item, node_name);
2019 /* create the tree and print */
2020 for (j = 0; j < LNET_NRBPOOLS; j++) {
2021 type_node = cYAML_create_object(cpt, pools[j]);
2022 if (type_node == NULL)
2024 if (cYAML_create_number(type_node, "npages",
2025 pool_cfg->pl_pools[j].pl_npages)
2028 if (cYAML_create_number(type_node, "nbuffers",
2029 pool_cfg->pl_pools[j].
2030 pl_nbuffers) == NULL)
2032 if (cYAML_create_number(type_node, "credits",
2033 pool_cfg->pl_pools[j].
2034 pl_credits) == NULL)
2036 if (cYAML_create_number(type_node, "mincredits",
2037 pool_cfg->pl_pools[j].
2038 pl_mincredits) == NULL)
2040 /* keep track of the total count for each of the
2041 * tiny, small and large buffers */
2042 buf_count[j] += pool_cfg->pl_pools[j].pl_nbuffers;
2046 if (pool_cfg != NULL) {
2047 item = cYAML_create_seq_item(pools_node);
2051 if (cYAML_create_number(item, "enable", pool_cfg->pl_routing) ==
2056 /* create a buffers entry in the show. This is necessary so that
2057 * if the YAML output is used to configure a node, the buffer
2058 * configuration takes hold */
2059 buffers = cYAML_create_object(root, "buffers");
2060 if (buffers == NULL)
2063 for (i = 0; i < LNET_NRBPOOLS; i++) {
2064 if (cYAML_create_number(buffers, pools[i], buf_count[i]) == NULL)
2068 if (show_rc == NULL)
2069 cYAML_print_tree(root);
2071 if (l_errno != ENOENT) {
2074 "\"cannot get routing information: %s\"",
2079 rc = LUSTRE_CFG_RC_NO_ERR;
2081 snprintf(err_str, sizeof(err_str), "\"success\"");
2082 rc = LUSTRE_CFG_RC_NO_ERR;
2086 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2087 cYAML_free_tree(root);
2088 } else if (show_rc != NULL && *show_rc != NULL) {
2089 struct cYAML *routing_node;
2090 /* there should exist only one routing block and one
2091 * buffers block. If there already exists a previous one
2092 * then don't add another */
2093 routing_node = cYAML_get_object_item(*show_rc, "routing");
2094 if (routing_node == NULL) {
2095 cYAML_insert_sibling((*show_rc)->cy_child,
2099 cYAML_free_tree(root);
2105 cYAML_build_error(rc, seq_no, SHOW_CMD, "routing", err_str, err_rc);
2110 int lustre_lnet_show_peer(char *knid, int detail, int seq_no,
2111 struct cYAML **show_rc, struct cYAML **err_rc)
2114 * TODO: This function is changing in a future patch to accommodate
2115 * PEER_LIST and proper filtering on any nid of the peer
2117 struct lnet_ioctl_peer_cfg peer_info;
2118 struct lnet_peer_ni_credit_info *lpni_cri;
2119 struct lnet_ioctl_element_stats *lpni_stats;
2120 int rc = LUSTRE_CFG_RC_OUT_OF_MEM, ncpt = 0, i = 0, j = 0;
2122 struct cYAML *root = NULL, *peer = NULL, *peer_ni = NULL,
2123 *first_seq = NULL, *peer_root = NULL, *tmp = NULL;
2124 char err_str[LNET_MAX_STR_LEN];
2125 lnet_nid_t prev_primary_nid = LNET_NID_ANY, primary_nid = LNET_NID_ANY;
2126 int data_size = sizeof(*lpni_cri) + sizeof(*lpni_stats);
2127 char *data = malloc(data_size);
2128 bool new_peer = true;
2130 snprintf(err_str, sizeof(err_str),
2131 "\"out of memory\"");
2136 /* create struct cYAML root object */
2137 root = cYAML_create_object(NULL, NULL);
2141 peer_root = cYAML_create_seq(root, "peer");
2142 if (peer_root == NULL)
2146 primary_nid = libcfs_str2nid(knid);
2150 memset(data, 0, data_size);
2151 memset(&peer_info, 0, sizeof(peer_info));
2152 LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2153 peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2154 peer_info.prcfg_count = i;
2155 peer_info.prcfg_bulk = (void *)data;
2156 peer_info.prcfg_size = data_size;
2158 rc = l_ioctl(LNET_DEV_ID,
2159 IOC_LIBCFS_GET_PEER_NI, &peer_info);
2165 if (primary_nid != LNET_NID_ANY &&
2166 primary_nid != peer_info.prcfg_prim_nid)
2169 lpni_cri = peer_info.prcfg_bulk;
2170 lpni_stats = peer_info.prcfg_bulk + sizeof(*lpni_cri);
2172 peer = cYAML_create_seq_item(peer_root);
2176 if (peer_info.prcfg_prim_nid != prev_primary_nid) {
2177 prev_primary_nid = peer_info.prcfg_prim_nid;
2182 lnet_nid_t pnid = peer_info.prcfg_prim_nid;
2183 if (cYAML_create_string(peer, "primary nid",
2184 libcfs_nid2str(pnid))
2187 if (cYAML_create_string(peer, "Multi-Rail",
2188 peer_info.prcfg_mr ?
2192 tmp = cYAML_create_seq(peer, "peer ni");
2198 if (first_seq == NULL)
2201 peer_ni = cYAML_create_seq_item(tmp);
2202 if (peer_ni == NULL)
2205 if (cYAML_create_string(peer_ni, "nid",
2207 (peer_info.prcfg_cfg_nid))
2211 if (cYAML_create_string(peer_ni, "state",
2212 lpni_cri->cr_aliveness)
2219 if (cYAML_create_number(peer_ni, "max_ni_tx_credits",
2220 lpni_cri->cr_ni_peer_tx_credits)
2224 if (cYAML_create_number(peer_ni, "available_tx_credits",
2225 lpni_cri->cr_peer_tx_credits)
2229 if (cYAML_create_number(peer_ni, "min_tx_credits",
2230 lpni_cri->cr_peer_min_tx_credits)
2234 if (cYAML_create_number(peer_ni, "tx_q_num_of_buf",
2235 lpni_cri->cr_peer_tx_qnob)
2239 if (cYAML_create_number(peer_ni, "available_rtr_credits",
2240 lpni_cri->cr_peer_rtr_credits)
2244 if (cYAML_create_number(peer_ni, "min_rtr_credits",
2245 lpni_cri->cr_peer_min_rtr_credits)
2249 if (cYAML_create_number(peer_ni, "send_count",
2250 lpni_stats->iel_send_count)
2254 if (cYAML_create_number(peer_ni, "recv_count",
2255 lpni_stats->iel_recv_count)
2259 if (cYAML_create_number(peer_ni, "drop_count",
2260 lpni_stats->iel_drop_count)
2264 if (cYAML_create_number(peer_ni, "refcount",
2265 lpni_cri->cr_refcount) == NULL)
2269 if (l_errno != ENOENT) {
2272 "\"cannot get peer information: %s\"",
2281 /* print output iff show_rc is not provided */
2282 if (show_rc == NULL)
2283 cYAML_print_tree(root);
2285 snprintf(err_str, sizeof(err_str), "\"success\"");
2286 rc = LUSTRE_CFG_RC_NO_ERR;
2289 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2290 cYAML_free_tree(root);
2291 } else if (show_rc != NULL && *show_rc != NULL) {
2292 struct cYAML *show_node;
2293 /* find the peer node, if one doesn't exist then
2294 * insert one. Otherwise add to the one there
2296 show_node = cYAML_get_object_item(*show_rc,
2298 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2299 cYAML_insert_child(show_node, first_seq);
2302 } else if (show_node == NULL) {
2303 cYAML_insert_sibling((*show_rc)->cy_child,
2307 cYAML_free_tree(root);
2313 cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
2319 static void add_to_global(struct cYAML *show_rc, struct cYAML *node,
2322 struct cYAML *show_node;
2324 show_node = cYAML_get_object_item(show_rc, "global");
2325 if (show_node != NULL)
2326 cYAML_insert_sibling(show_node->cy_child,
2329 cYAML_insert_sibling(show_rc->cy_child,
2334 int lustre_lnet_show_max_intf(int seq_no, struct cYAML **show_rc,
2335 struct cYAML **err_rc)
2337 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2338 char val[LNET_MAX_STR_LEN];
2340 char err_str[LNET_MAX_STR_LEN];
2341 struct cYAML *root = NULL, *global = NULL;
2343 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2345 rc = read_sysfs_file(modparam_path, "lnet_interfaces_max", val,
2346 1, LNET_MAX_STR_LEN);
2348 snprintf(err_str, sizeof(err_str),
2349 "\"cannot get max interfaces: %d\"", rc);
2353 max_intf = atoi(val);
2355 root = cYAML_create_object(NULL, NULL);
2359 global = cYAML_create_object(root, "global");
2363 if (cYAML_create_number(global, "max_intf",
2367 if (show_rc == NULL)
2368 cYAML_print_tree(root);
2370 snprintf(err_str, sizeof(err_str), "\"success\"");
2372 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR)
2373 cYAML_free_tree(root);
2374 else if (show_rc != NULL && *show_rc != NULL)
2375 add_to_global(*show_rc, global, root);
2379 cYAML_build_error(rc, seq_no, SHOW_CMD, "global", err_str, err_rc);
2384 int lustre_lnet_show_discovery(int seq_no, struct cYAML **show_rc,
2385 struct cYAML **err_rc)
2387 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2388 char val[LNET_MAX_STR_LEN];
2390 char err_str[LNET_MAX_STR_LEN];
2391 struct cYAML *root = NULL, *global = NULL;
2393 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2395 rc = read_sysfs_file(modparam_path, "lnet_peer_discovery_disabled", val,
2398 snprintf(err_str, sizeof(err_str),
2399 "\"cannot get discovery value: %d\"", rc);
2404 * the sysfs parameter read indicates if discovery is disabled,
2405 * but we report if discovery is enabled.
2407 discovery = !atoi(val);
2409 root = cYAML_create_object(NULL, NULL);
2413 global = cYAML_create_object(root, "global");
2417 if (cYAML_create_number(global, "discovery",
2421 if (show_rc == NULL)
2422 cYAML_print_tree(root);
2424 snprintf(err_str, sizeof(err_str), "\"success\"");
2426 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2427 cYAML_free_tree(root);
2428 } else if (show_rc != NULL && *show_rc != NULL) {
2429 add_to_global(*show_rc, global, root);
2434 cYAML_build_error(rc, seq_no, SHOW_CMD, "global", err_str, err_rc);
2439 int lustre_lnet_show_numa_range(int seq_no, struct cYAML **show_rc,
2440 struct cYAML **err_rc)
2442 struct lnet_ioctl_set_value data;
2445 char err_str[LNET_MAX_STR_LEN];
2446 struct cYAML *root = NULL, *global = NULL;
2448 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2450 LIBCFS_IOC_INIT_V2(data, sv_hdr);
2452 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NUMA_RANGE, &data);
2457 "\"cannot get numa range: %s\"",
2463 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2465 root = cYAML_create_object(NULL, NULL);
2469 global = cYAML_create_object(root, "global");
2473 if (cYAML_create_number(global, "numa_range",
2474 data.sv_value) == NULL)
2477 if (show_rc == NULL)
2478 cYAML_print_tree(root);
2480 snprintf(err_str, sizeof(err_str), "\"success\"");
2481 rc = LUSTRE_CFG_RC_NO_ERR;
2483 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2484 cYAML_free_tree(root);
2485 } else if (show_rc != NULL && *show_rc != NULL) {
2486 add_to_global(*show_rc, global, root);
2491 cYAML_build_error(rc, seq_no, SHOW_CMD, "global", err_str, err_rc);
2496 int lustre_lnet_show_stats(int seq_no, struct cYAML **show_rc,
2497 struct cYAML **err_rc)
2499 struct lnet_ioctl_lnet_stats data;
2502 char err_str[LNET_MAX_STR_LEN];
2503 struct cYAML *root = NULL, *stats = NULL;
2505 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2507 LIBCFS_IOC_INIT_V2(data, st_hdr);
2509 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LNET_STATS, &data);
2514 "\"cannot get lnet statistics: %s\"",
2520 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2522 root = cYAML_create_object(NULL, NULL);
2526 stats = cYAML_create_object(root, "statistics");
2530 if (cYAML_create_number(stats, "msgs_alloc",
2531 data.st_cntrs.msgs_alloc) == NULL)
2534 if (cYAML_create_number(stats, "msgs_max",
2535 data.st_cntrs.msgs_max) == NULL)
2538 if (cYAML_create_number(stats, "errors",
2539 data.st_cntrs.errors) == NULL)
2542 if (cYAML_create_number(stats, "send_count",
2543 data.st_cntrs.send_count) == NULL)
2546 if (cYAML_create_number(stats, "recv_count",
2547 data.st_cntrs.recv_count) == NULL)
2550 if (cYAML_create_number(stats, "route_count",
2551 data.st_cntrs.route_count) == NULL)
2554 if (cYAML_create_number(stats, "drop_count",
2555 data.st_cntrs.drop_count) == NULL)
2558 if (cYAML_create_number(stats, "send_length",
2559 data.st_cntrs.send_length) == NULL)
2562 if (cYAML_create_number(stats, "recv_length",
2563 data.st_cntrs.recv_length) == NULL)
2566 if (cYAML_create_number(stats, "route_length",
2567 data.st_cntrs.route_length) == NULL)
2570 if (cYAML_create_number(stats, "drop_length",
2571 data.st_cntrs.drop_length) == NULL)
2574 if (show_rc == NULL)
2575 cYAML_print_tree(root);
2577 snprintf(err_str, sizeof(err_str), "\"success\"");
2578 rc = LUSTRE_CFG_RC_NO_ERR;
2580 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2581 cYAML_free_tree(root);
2582 } else if (show_rc != NULL && *show_rc != NULL) {
2583 cYAML_insert_sibling((*show_rc)->cy_child,
2590 cYAML_build_error(rc, seq_no, SHOW_CMD, "statistics", err_str, err_rc);
2595 typedef int (*cmd_handler_t)(struct cYAML *tree,
2596 struct cYAML **show_rc,
2597 struct cYAML **err_rc);
2599 static int handle_yaml_config_route(struct cYAML *tree, struct cYAML **show_rc,
2600 struct cYAML **err_rc)
2602 struct cYAML *net, *gw, *hop, *prio, *seq_no;
2604 net = cYAML_get_object_item(tree, "net");
2605 gw = cYAML_get_object_item(tree, "gateway");
2606 hop = cYAML_get_object_item(tree, "hop");
2607 prio = cYAML_get_object_item(tree, "priority");
2608 seq_no = cYAML_get_object_item(tree, "seq_no");
2610 return lustre_lnet_config_route((net) ? net->cy_valuestring : NULL,
2611 (gw) ? gw->cy_valuestring : NULL,
2612 (hop) ? hop->cy_valueint : -1,
2613 (prio) ? prio->cy_valueint : -1,
2614 (seq_no) ? seq_no->cy_valueint : -1,
2618 static void yaml_free_string_array(char **array, int num)
2621 char **sub_array = array;
2623 for (i = 0; i < num; i++) {
2624 if (*sub_array != NULL)
2634 * 0: <intf_name>['['<expr>']']
2635 * 1: <intf_name>['['<expr>']']
2637 static int yaml_copy_intf_info(struct cYAML *intf_tree,
2638 struct lnet_dlc_network_descr *nw_descr)
2640 struct cYAML *child = NULL;
2641 int intf_num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
2642 struct lnet_dlc_intf_descr *intf_descr, *tmp;
2644 if (intf_tree == NULL || nw_descr == NULL)
2645 return LUSTRE_CFG_RC_BAD_PARAM;
2647 /* now grab all the interfaces and their cpts */
2648 child = intf_tree->cy_child;
2649 while (child != NULL) {
2650 if (child->cy_valuestring == NULL) {
2651 child = child->cy_next;
2655 if (strlen(child->cy_valuestring) >= LNET_MAX_STR_LEN)
2658 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist,
2659 child->cy_valuestring,
2660 strlen(child->cy_valuestring));
2661 if (rc != LUSTRE_CFG_RC_NO_ERR)
2665 child = child->cy_next;
2669 return LUSTRE_CFG_RC_MISSING_PARAM;
2674 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
2676 list_del(&intf_descr->intf_on_network);
2677 free_intf_descr(intf_descr);
2684 yaml_extract_cmn_tunables(struct cYAML *tree,
2685 struct lnet_ioctl_config_lnd_cmn_tunables *tunables,
2686 struct cfs_expr_list **global_cpts)
2688 struct cYAML *tun, *item, *smp;
2691 tun = cYAML_get_object_item(tree, "tunables");
2693 item = cYAML_get_object_item(tun, "peer_timeout");
2695 tunables->lct_peer_timeout = item->cy_valueint;
2696 item = cYAML_get_object_item(tun, "peer_credits");
2698 tunables->lct_peer_tx_credits = item->cy_valueint;
2699 item = cYAML_get_object_item(tun, "peer_buffer_credits");
2701 tunables->lct_peer_rtr_credits = item->cy_valueint;
2702 item = cYAML_get_object_item(tun, "credits");
2704 tunables->lct_max_tx_credits = item->cy_valueint;
2705 smp = cYAML_get_object_item(tun, "CPT");
2707 rc = cfs_expr_list_parse(smp->cy_valuestring,
2708 strlen(smp->cy_valuestring),
2709 0, UINT_MAX, global_cpts);
2711 *global_cpts = NULL;
2721 yaml_extract_tunables(struct cYAML *tree,
2722 struct lnet_ioctl_config_lnd_tunables *tunables,
2723 struct cfs_expr_list **global_cpts,
2728 rc = yaml_extract_cmn_tunables(tree, &tunables->lt_cmn,
2734 lustre_yaml_extract_lnd_tunables(tree, net_type,
2742 * - net type: <net>[<NUM>]
2744 * - nid: <ip>@<net>[<NUM>]
2747 * 0: <intf_name>['['<expr>']']
2748 * 1: <intf_name>['['<expr>']']
2750 * peer_timeout: <NUM>
2751 * peer_credits: <NUM>
2752 * peer_buffer_credits: <NUM>
2755 * peercredits_hiw: <NUM>
2756 * map_on_demand: <NUM>
2757 * concurrent_sends: <NUM>
2758 * fmr_pool_size: <NUM>
2759 * fmr_flush_trigger: <NUM>
2762 * At least one interface is required. If no interfaces are provided the
2763 * network interface can not be configured.
2765 static int handle_yaml_config_ni(struct cYAML *tree, struct cYAML **show_rc,
2766 struct cYAML **err_rc)
2768 struct cYAML *net, *intf, *seq_no, *ip2net = NULL, *local_nis = NULL,
2770 int num_entries = 0, rc;
2771 struct lnet_dlc_network_descr nw_descr;
2772 struct cfs_expr_list *global_cpts = NULL;
2773 struct lnet_ioctl_config_lnd_tunables tunables;
2776 memset(&tunables, 0, sizeof(tunables));
2778 INIT_LIST_HEAD(&nw_descr.network_on_rule);
2779 INIT_LIST_HEAD(&nw_descr.nw_intflist);
2781 ip2net = cYAML_get_object_item(tree, "ip2net");
2782 net = cYAML_get_object_item(tree, "net type");
2784 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
2786 nw_descr.nw_id = LOLND;
2789 * if neither net nor ip2nets are present, then we can not
2790 * configure the network.
2792 if (!net && !ip2net)
2793 return LUSTRE_CFG_RC_MISSING_PARAM;
2795 local_nis = cYAML_get_object_item(tree, "local NI(s)");
2796 if (local_nis == NULL)
2797 return LUSTRE_CFG_RC_MISSING_PARAM;
2799 if (!cYAML_is_sequence(local_nis))
2800 return LUSTRE_CFG_RC_BAD_PARAM;
2802 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
2803 intf = cYAML_get_object_item(item, "interfaces");
2806 num_entries = yaml_copy_intf_info(intf, &nw_descr);
2807 if (num_entries <= 0) {
2808 cYAML_build_error(num_entries, -1, "ni", "add",
2809 "bad interface list",
2811 return LUSTRE_CFG_RC_BAD_PARAM;
2815 found = yaml_extract_tunables(tree, &tunables, &global_cpts,
2816 LNET_NETTYP(nw_descr.nw_id));
2817 seq_no = cYAML_get_object_item(tree, "seq_no");
2819 rc = lustre_lnet_config_ni(&nw_descr,
2821 (ip2net) ? ip2net->cy_valuestring : NULL,
2822 (found) ? &tunables: NULL,
2823 (seq_no) ? seq_no->cy_valueint : -1,
2826 if (global_cpts != NULL)
2827 cfs_expr_list_free(global_cpts);
2834 * - net-spec: <tcp|o2ib|gni>[NUM]
2836 * 0: <intf name>['['<expr>']']
2837 * 1: <intf name>['['<expr>']']
2839 * 0: <expr.expr.expr.expr>
2840 * 1: <expr.expr.expr.expr>
2842 static int handle_yaml_config_ip2nets(struct cYAML *tree,
2843 struct cYAML **show_rc,
2844 struct cYAML **err_rc)
2846 struct cYAML *net, *ip_range, *item = NULL, *intf = NULL,
2848 struct lustre_lnet_ip2nets ip2nets;
2849 struct lustre_lnet_ip_range_descr *ip_range_descr = NULL,
2851 int rc = LUSTRE_CFG_RC_NO_ERR;
2852 struct cfs_expr_list *global_cpts = NULL;
2853 struct cfs_expr_list *el, *el_tmp;
2854 struct lnet_ioctl_config_lnd_tunables tunables;
2855 struct lnet_dlc_intf_descr *intf_descr, *intf_tmp;
2858 memset(&tunables, 0, sizeof(tunables));
2860 /* initialize all lists */
2861 INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
2862 INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
2863 INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
2865 net = cYAML_get_object_item(tree, "net-spec");
2867 return LUSTRE_CFG_RC_BAD_PARAM;
2869 if (net != NULL && net->cy_valuestring == NULL)
2870 return LUSTRE_CFG_RC_BAD_PARAM;
2872 /* assign the network id */
2873 ip2nets.ip2nets_net.nw_id = libcfs_str2net(net->cy_valuestring);
2874 if (ip2nets.ip2nets_net.nw_id == LNET_NID_ANY)
2875 return LUSTRE_CFG_RC_BAD_PARAM;
2877 seq_no = cYAML_get_object_item(tree, "seq_no");
2879 intf = cYAML_get_object_item(tree, "interfaces");
2881 rc = yaml_copy_intf_info(intf, &ip2nets.ip2nets_net);
2883 return LUSTRE_CFG_RC_BAD_PARAM;
2886 ip_range = cYAML_get_object_item(tree, "ip-range");
2887 if (ip_range != NULL) {
2888 item = ip_range->cy_child;
2889 while (item != NULL) {
2890 if (item->cy_valuestring == NULL) {
2891 item = item->cy_next;
2895 rc = lustre_lnet_add_ip_range(&ip2nets.ip2nets_ip_ranges,
2896 item->cy_valuestring);
2898 if (rc != LUSTRE_CFG_RC_NO_ERR)
2901 item = item->cy_next;
2905 found = yaml_extract_tunables(tree, &tunables, &global_cpts,
2906 LNET_NETTYP(ip2nets.ip2nets_net.nw_id));
2908 rc = lustre_lnet_config_ip2nets(&ip2nets,
2909 (found) ? &tunables : NULL,
2911 (seq_no) ? seq_no->cy_valueint : -1,
2915 * don't stop because there was no match. Continue processing the
2916 * rest of the rules. If non-match then nothing is configured
2918 if (rc == LUSTRE_CFG_RC_NO_MATCH)
2919 rc = LUSTRE_CFG_RC_NO_ERR;
2921 list_for_each_entry_safe(intf_descr, intf_tmp,
2922 &ip2nets.ip2nets_net.nw_intflist,
2924 list_del(&intf_descr->intf_on_network);
2925 free_intf_descr(intf_descr);
2928 list_for_each_entry_safe(ip_range_descr, tmp,
2929 &ip2nets.ip2nets_ip_ranges,
2931 list_del(&ip_range_descr->ipr_entry);
2932 list_for_each_entry_safe(el, el_tmp, &ip_range_descr->ipr_expr,
2934 list_del(&el->el_link);
2935 cfs_expr_list_free(el);
2937 free(ip_range_descr);
2943 static int handle_yaml_del_ni(struct cYAML *tree, struct cYAML **show_rc,
2944 struct cYAML **err_rc)
2946 struct cYAML *net = NULL, *intf = NULL, *seq_no = NULL, *item = NULL,
2948 int num_entries, rc;
2949 struct lnet_dlc_network_descr nw_descr;
2951 INIT_LIST_HEAD(&nw_descr.network_on_rule);
2952 INIT_LIST_HEAD(&nw_descr.nw_intflist);
2954 net = cYAML_get_object_item(tree, "net type");
2956 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
2958 local_nis = cYAML_get_object_item(tree, "local NI(s)");
2959 if (local_nis == NULL)
2960 return LUSTRE_CFG_RC_MISSING_PARAM;
2962 if (!cYAML_is_sequence(local_nis))
2963 return LUSTRE_CFG_RC_BAD_PARAM;
2965 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
2966 intf = cYAML_get_object_item(item, "interfaces");
2969 num_entries = yaml_copy_intf_info(intf, &nw_descr);
2970 if (num_entries <= 0) {
2971 cYAML_build_error(num_entries, -1, "ni", "add",
2972 "bad interface list",
2974 return LUSTRE_CFG_RC_BAD_PARAM;
2978 seq_no = cYAML_get_object_item(tree, "seq_no");
2980 rc = lustre_lnet_del_ni((net) ? &nw_descr : NULL,
2981 (seq_no) ? seq_no->cy_valueint : -1,
2987 static int yaml_copy_peer_nids(struct cYAML *tree, char ***nidsppp, bool del)
2989 struct cYAML *nids_entry = NULL, *child = NULL, *entry = NULL,
2992 int num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
2994 prim_nid = cYAML_get_object_item(tree, "primary nid");
2995 if (!prim_nid || !prim_nid->cy_valuestring)
2996 return LUSTRE_CFG_RC_MISSING_PARAM;
2998 nids_entry = cYAML_get_object_item(tree, "peer ni");
2999 if (cYAML_is_sequence(nids_entry)) {
3000 while (cYAML_get_next_seq_item(nids_entry, &child)) {
3001 entry = cYAML_get_object_item(child, "nid");
3002 /* don't count an empty entry */
3003 if (!entry || !entry->cy_valuestring)
3006 if ((strcmp(entry->cy_valuestring, prim_nid->cy_valuestring)
3009 * primary nid is present in the list of
3010 * nids so that means we want to delete
3011 * the entire peer, so no need to go
3012 * further. Just delete the entire peer.
3022 return LUSTRE_CFG_RC_MISSING_PARAM;
3024 nids = calloc(sizeof(*nids) * num, 1);
3026 return LUSTRE_CFG_RC_OUT_OF_MEM;
3028 /* now grab all the nids */
3031 while (cYAML_get_next_seq_item(nids_entry, &child)) {
3032 entry = cYAML_get_object_item(child, "nid");
3033 if (!entry || !entry->cy_valuestring)
3036 nids[num] = calloc(strlen(entry->cy_valuestring) + 1, 1);
3038 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3041 strncpy(nids[num], entry->cy_valuestring,
3042 strlen(entry->cy_valuestring));
3052 yaml_free_string_array(nids, num);
3057 static int handle_yaml_config_peer(struct cYAML *tree, struct cYAML **show_rc,
3058 struct cYAML **err_rc)
3062 struct cYAML *seq_no, *prim_nid, *non_mr;
3064 num = yaml_copy_peer_nids(tree, &nids, false);
3068 seq_no = cYAML_get_object_item(tree, "seq_no");
3069 prim_nid = cYAML_get_object_item(tree, "primary nid");
3070 non_mr = cYAML_get_object_item(tree, "non_mr");
3072 rc = lustre_lnet_config_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
3074 (non_mr) ? false : true,
3075 (seq_no) ? seq_no->cy_valueint : -1,
3078 yaml_free_string_array(nids, num);
3082 static int handle_yaml_del_peer(struct cYAML *tree, struct cYAML **show_rc,
3083 struct cYAML **err_rc)
3087 struct cYAML *seq_no, *prim_nid;
3089 num = yaml_copy_peer_nids(tree, &nids, true);
3093 seq_no = cYAML_get_object_item(tree, "seq_no");
3094 prim_nid = cYAML_get_object_item(tree, "primary nid");
3096 rc = lustre_lnet_del_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
3098 (seq_no) ? seq_no->cy_valueint : -1,
3101 yaml_free_string_array(nids, num);
3105 static int handle_yaml_config_buffers(struct cYAML *tree,
3106 struct cYAML **show_rc,
3107 struct cYAML **err_rc)
3110 struct cYAML *tiny, *small, *large, *seq_no;
3112 tiny = cYAML_get_object_item(tree, "tiny");
3113 small = cYAML_get_object_item(tree, "small");
3114 large = cYAML_get_object_item(tree, "large");
3115 seq_no = cYAML_get_object_item(tree, "seq_no");
3117 rc = lustre_lnet_config_buffers((tiny) ? tiny->cy_valueint : -1,
3118 (small) ? small->cy_valueint : -1,
3119 (large) ? large->cy_valueint : -1,
3120 (seq_no) ? seq_no->cy_valueint : -1,
3126 static int handle_yaml_config_routing(struct cYAML *tree,
3127 struct cYAML **show_rc,
3128 struct cYAML **err_rc)
3130 int rc = LUSTRE_CFG_RC_NO_ERR;
3131 struct cYAML *seq_no, *enable;
3133 seq_no = cYAML_get_object_item(tree, "seq_no");
3134 enable = cYAML_get_object_item(tree, "enable");
3137 rc = lustre_lnet_enable_routing(enable->cy_valueint,
3139 seq_no->cy_valueint : -1,
3146 static int handle_yaml_del_route(struct cYAML *tree, struct cYAML **show_rc,
3147 struct cYAML **err_rc)
3151 struct cYAML *seq_no;
3153 net = cYAML_get_object_item(tree, "net");
3154 gw = cYAML_get_object_item(tree, "gateway");
3155 seq_no = cYAML_get_object_item(tree, "seq_no");
3157 return lustre_lnet_del_route((net) ? net->cy_valuestring : NULL,
3158 (gw) ? gw->cy_valuestring : NULL,
3159 (seq_no) ? seq_no->cy_valueint : -1,
3163 static int handle_yaml_del_routing(struct cYAML *tree, struct cYAML **show_rc,
3164 struct cYAML **err_rc)
3166 struct cYAML *seq_no;
3168 seq_no = cYAML_get_object_item(tree, "seq_no");
3170 return lustre_lnet_enable_routing(0, (seq_no) ?
3171 seq_no->cy_valueint : -1,
3175 static int handle_yaml_show_route(struct cYAML *tree, struct cYAML **show_rc,
3176 struct cYAML **err_rc)
3182 struct cYAML *detail;
3183 struct cYAML *seq_no;
3185 net = cYAML_get_object_item(tree, "net");
3186 gw = cYAML_get_object_item(tree, "gateway");
3187 hop = cYAML_get_object_item(tree, "hop");
3188 prio = cYAML_get_object_item(tree, "priority");
3189 detail = cYAML_get_object_item(tree, "detail");
3190 seq_no = cYAML_get_object_item(tree, "seq_no");
3192 return lustre_lnet_show_route((net) ? net->cy_valuestring : NULL,
3193 (gw) ? gw->cy_valuestring : NULL,
3194 (hop) ? hop->cy_valueint : -1,
3195 (prio) ? prio->cy_valueint : -1,
3196 (detail) ? detail->cy_valueint : 0,
3197 (seq_no) ? seq_no->cy_valueint : -1,
3202 static int handle_yaml_show_net(struct cYAML *tree, struct cYAML **show_rc,
3203 struct cYAML **err_rc)
3205 struct cYAML *net, *detail, *seq_no;
3207 net = cYAML_get_object_item(tree, "net");
3208 detail = cYAML_get_object_item(tree, "detail");
3209 seq_no = cYAML_get_object_item(tree, "seq_no");
3211 return lustre_lnet_show_net((net) ? net->cy_valuestring : NULL,
3212 (detail) ? detail->cy_valueint : 0,
3213 (seq_no) ? seq_no->cy_valueint : -1,
3218 static int handle_yaml_show_routing(struct cYAML *tree, struct cYAML **show_rc,
3219 struct cYAML **err_rc)
3221 struct cYAML *seq_no;
3223 seq_no = cYAML_get_object_item(tree, "seq_no");
3225 return lustre_lnet_show_routing((seq_no) ? seq_no->cy_valueint : -1,
3229 static int handle_yaml_show_peers(struct cYAML *tree, struct cYAML **show_rc,
3230 struct cYAML **err_rc)
3232 struct cYAML *seq_no, *nid, *detail;
3234 seq_no = cYAML_get_object_item(tree, "seq_no");
3235 detail = cYAML_get_object_item(tree, "detail");
3236 nid = cYAML_get_object_item(tree, "nid");
3238 return lustre_lnet_show_peer((nid) ? nid->cy_valuestring : NULL,
3239 (detail) ? detail->cy_valueint : 0,
3240 (seq_no) ? seq_no->cy_valueint : -1,
3244 static int handle_yaml_show_stats(struct cYAML *tree, struct cYAML **show_rc,
3245 struct cYAML **err_rc)
3247 struct cYAML *seq_no;
3249 seq_no = cYAML_get_object_item(tree, "seq_no");
3251 return lustre_lnet_show_stats((seq_no) ? seq_no->cy_valueint : -1,
3255 static int handle_yaml_config_global_settings(struct cYAML *tree,
3256 struct cYAML **show_rc,
3257 struct cYAML **err_rc)
3259 struct cYAML *max_intf, *numa, *discovery, *seq_no;
3262 seq_no = cYAML_get_object_item(tree, "seq_no");
3263 max_intf = cYAML_get_object_item(tree, "max_intf");
3265 rc = lustre_lnet_config_max_intf(max_intf->cy_valueint,
3266 seq_no ? seq_no->cy_valueint
3270 numa = cYAML_get_object_item(tree, "numa_range");
3272 rc = lustre_lnet_config_numa_range(numa->cy_valueint,
3273 seq_no ? seq_no->cy_valueint
3277 discovery = cYAML_get_object_item(tree, "discovery");
3279 rc = lustre_lnet_config_discovery(discovery->cy_valueint,
3280 seq_no ? seq_no->cy_valueint
3287 static int handle_yaml_del_global_settings(struct cYAML *tree,
3288 struct cYAML **show_rc,
3289 struct cYAML **err_rc)
3291 struct cYAML *max_intf, *numa, *discovery, *seq_no;
3294 seq_no = cYAML_get_object_item(tree, "seq_no");
3295 max_intf = cYAML_get_object_item(tree, "max_intf");
3297 rc = lustre_lnet_config_max_intf(LNET_INTERFACES_MAX_DEFAULT,
3298 seq_no ? seq_no->cy_valueint
3302 numa = cYAML_get_object_item(tree, "numa_range");
3304 rc = lustre_lnet_config_numa_range(0,
3305 seq_no ? seq_no->cy_valueint
3309 /* peer discovery is enabled by default */
3310 discovery = cYAML_get_object_item(tree, "discovery");
3312 rc = lustre_lnet_config_discovery(1,
3313 seq_no ? seq_no->cy_valueint
3320 static int handle_yaml_show_global_settings(struct cYAML *tree,
3321 struct cYAML **show_rc,
3322 struct cYAML **err_rc)
3324 struct cYAML *max_intf, *numa, *discovery, *seq_no;
3327 seq_no = cYAML_get_object_item(tree, "seq_no");
3328 max_intf = cYAML_get_object_item(tree, "max_intf");
3330 rc = lustre_lnet_show_max_intf(seq_no ? seq_no->cy_valueint
3334 numa = cYAML_get_object_item(tree, "numa_range");
3336 rc = lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint
3340 discovery = cYAML_get_object_item(tree, "discovery");
3342 rc = lustre_lnet_show_discovery(seq_no ? seq_no->cy_valueint
3349 struct lookup_cmd_hdlr_tbl {
3354 static struct lookup_cmd_hdlr_tbl lookup_config_tbl[] = {
3355 { .name = "route", .cb = handle_yaml_config_route },
3356 { .name = "net", .cb = handle_yaml_config_ni },
3357 { .name = "ip2nets", .cb = handle_yaml_config_ip2nets },
3358 { .name = "peer", .cb = handle_yaml_config_peer },
3359 { .name = "routing", .cb = handle_yaml_config_routing },
3360 { .name = "buffers", .cb = handle_yaml_config_buffers },
3361 { .name = "global", .cb = handle_yaml_config_global_settings},
3364 static struct lookup_cmd_hdlr_tbl lookup_del_tbl[] = {
3365 { .name = "route", .cb = handle_yaml_del_route },
3366 { .name = "net", .cb = handle_yaml_del_ni },
3367 { .name = "peer", .cb = handle_yaml_del_peer },
3368 { .name = "routing", .cb = handle_yaml_del_routing },
3369 { .name = "global", .cb = handle_yaml_del_global_settings},
3372 static struct lookup_cmd_hdlr_tbl lookup_show_tbl[] = {
3373 { .name = "route", .cb = handle_yaml_show_route },
3374 { .name = "net", .cb = handle_yaml_show_net },
3375 { .name = "buffers", .cb = handle_yaml_show_routing },
3376 { .name = "routing", .cb = handle_yaml_show_routing },
3377 { .name = "peer", .cb = handle_yaml_show_peers },
3378 { .name = "statistics", .cb = handle_yaml_show_stats },
3379 { .name = "global", .cb = handle_yaml_show_global_settings},
3382 static cmd_handler_t lookup_fn(char *key,
3383 struct lookup_cmd_hdlr_tbl *tbl)
3389 for (i = 0; tbl[i].name != NULL; i++) {
3390 if (strncmp(key, tbl[i].name, strlen(tbl[i].name)) == 0)
3397 static int lustre_yaml_cb_helper(char *f, struct lookup_cmd_hdlr_tbl *table,
3398 struct cYAML **show_rc, struct cYAML **err_rc)
3400 struct cYAML *tree, *item = NULL, *head, *child;
3402 char err_str[LNET_MAX_STR_LEN];
3403 int rc = LUSTRE_CFG_RC_NO_ERR, return_rc = LUSTRE_CFG_RC_NO_ERR;
3405 tree = cYAML_build_tree(f, NULL, 0, err_rc, false);
3407 return LUSTRE_CFG_RC_BAD_PARAM;
3409 child = tree->cy_child;
3410 while (child != NULL) {
3411 cb = lookup_fn(child->cy_string, table);
3413 snprintf(err_str, sizeof(err_str),
3414 "\"call back for '%s' not found\"",
3416 cYAML_build_error(LUSTRE_CFG_RC_BAD_PARAM, -1,
3417 "yaml", "helper", err_str, err_rc);
3421 if (cYAML_is_sequence(child)) {
3422 while ((head = cYAML_get_next_seq_item(child, &item))
3424 rc = cb(head, show_rc, err_rc);
3425 if (rc != LUSTRE_CFG_RC_NO_ERR)
3429 rc = cb(child, show_rc, err_rc);
3430 if (rc != LUSTRE_CFG_RC_NO_ERR)
3434 child = child->cy_next;
3438 cYAML_free_tree(tree);
3443 int lustre_yaml_config(char *f, struct cYAML **err_rc)
3445 return lustre_yaml_cb_helper(f, lookup_config_tbl,
3449 int lustre_yaml_del(char *f, struct cYAML **err_rc)
3451 return lustre_yaml_cb_helper(f, lookup_del_tbl,
3455 int lustre_yaml_show(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
3457 return lustre_yaml_cb_helper(f, lookup_show_tbl,