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_numa_range(int range, int seq_no, struct cYAML **err_rc)
1864 struct lnet_ioctl_set_value data;
1865 int rc = LUSTRE_CFG_RC_NO_ERR;
1866 char err_str[LNET_MAX_STR_LEN];
1868 snprintf(err_str, sizeof(err_str), "\"success\"");
1873 "\"range must be >= 0\"");
1874 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1878 LIBCFS_IOC_INIT_V2(data, sv_hdr);
1879 data.sv_value = range;
1881 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_SET_NUMA_RANGE, &data);
1886 "\"cannot configure numa_range: %s\"", strerror(errno));
1891 cYAML_build_error(rc, seq_no, ADD_CMD, "numa_range", err_str, err_rc);
1896 int lustre_lnet_config_buffers(int tiny, int small, int large, int seq_no,
1897 struct cYAML **err_rc)
1899 struct lnet_ioctl_config_data data;
1900 int rc = LUSTRE_CFG_RC_NO_ERR;
1901 char err_str[LNET_MAX_STR_LEN];
1903 snprintf(err_str, sizeof(err_str), "\"success\"");
1905 /* -1 indicates to ignore changes to this field */
1906 if (tiny < -1 || small < -1 || large < -1) {
1909 "\"tiny, small and large must be >= 0\"");
1910 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1914 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1915 data.cfg_config_u.cfg_buffers.buf_tiny = tiny;
1916 data.cfg_config_u.cfg_buffers.buf_small = small;
1917 data.cfg_config_u.cfg_buffers.buf_large = large;
1919 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_BUF, &data);
1924 "\"cannot configure buffers: %s\"", strerror(errno));
1929 cYAML_build_error(rc, seq_no, ADD_CMD, "buf", err_str, err_rc);
1934 int lustre_lnet_show_routing(int seq_no, struct cYAML **show_rc,
1935 struct cYAML **err_rc)
1937 struct lnet_ioctl_config_data *data;
1938 struct lnet_ioctl_pool_cfg *pool_cfg = NULL;
1939 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1942 char *pools[LNET_NRBPOOLS] = {"tiny", "small", "large"};
1943 int buf_count[LNET_NRBPOOLS] = {0};
1944 struct cYAML *root = NULL, *pools_node = NULL,
1945 *type_node = NULL, *item = NULL, *cpt = NULL,
1946 *first_seq = NULL, *buffers = NULL;
1948 char err_str[LNET_MAX_STR_LEN];
1949 char node_name[LNET_MAX_STR_LEN];
1952 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1954 buf = calloc(1, sizeof(*data) + sizeof(*pool_cfg));
1958 data = (struct lnet_ioctl_config_data *)buf;
1960 root = cYAML_create_object(NULL, NULL);
1964 pools_node = cYAML_create_seq(root, "routing");
1965 if (pools_node == NULL)
1969 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
1970 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
1971 sizeof(struct lnet_ioctl_pool_cfg);
1972 data->cfg_count = i;
1974 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_BUF, data);
1982 pool_cfg = (struct lnet_ioctl_pool_cfg *)data->cfg_bulk;
1984 snprintf(node_name, sizeof(node_name), "cpt[%d]", i);
1985 item = cYAML_create_seq_item(pools_node);
1989 if (first_seq == NULL)
1992 cpt = cYAML_create_object(item, node_name);
1996 /* create the tree and print */
1997 for (j = 0; j < LNET_NRBPOOLS; j++) {
1998 type_node = cYAML_create_object(cpt, pools[j]);
1999 if (type_node == NULL)
2001 if (cYAML_create_number(type_node, "npages",
2002 pool_cfg->pl_pools[j].pl_npages)
2005 if (cYAML_create_number(type_node, "nbuffers",
2006 pool_cfg->pl_pools[j].
2007 pl_nbuffers) == NULL)
2009 if (cYAML_create_number(type_node, "credits",
2010 pool_cfg->pl_pools[j].
2011 pl_credits) == NULL)
2013 if (cYAML_create_number(type_node, "mincredits",
2014 pool_cfg->pl_pools[j].
2015 pl_mincredits) == NULL)
2017 /* keep track of the total count for each of the
2018 * tiny, small and large buffers */
2019 buf_count[j] += pool_cfg->pl_pools[j].pl_nbuffers;
2023 if (pool_cfg != NULL) {
2024 item = cYAML_create_seq_item(pools_node);
2028 if (cYAML_create_number(item, "enable", pool_cfg->pl_routing) ==
2033 /* create a buffers entry in the show. This is necessary so that
2034 * if the YAML output is used to configure a node, the buffer
2035 * configuration takes hold */
2036 buffers = cYAML_create_object(root, "buffers");
2037 if (buffers == NULL)
2040 for (i = 0; i < LNET_NRBPOOLS; i++) {
2041 if (cYAML_create_number(buffers, pools[i], buf_count[i]) == NULL)
2045 if (show_rc == NULL)
2046 cYAML_print_tree(root);
2048 if (l_errno != ENOENT) {
2051 "\"cannot get routing information: %s\"",
2056 rc = LUSTRE_CFG_RC_NO_ERR;
2058 snprintf(err_str, sizeof(err_str), "\"success\"");
2059 rc = LUSTRE_CFG_RC_NO_ERR;
2063 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2064 cYAML_free_tree(root);
2065 } else if (show_rc != NULL && *show_rc != NULL) {
2066 struct cYAML *routing_node;
2067 /* there should exist only one routing block and one
2068 * buffers block. If there already exists a previous one
2069 * then don't add another */
2070 routing_node = cYAML_get_object_item(*show_rc, "routing");
2071 if (routing_node == NULL) {
2072 cYAML_insert_sibling((*show_rc)->cy_child,
2076 cYAML_free_tree(root);
2082 cYAML_build_error(rc, seq_no, SHOW_CMD, "routing", err_str, err_rc);
2087 int lustre_lnet_show_peer(char *knid, int detail, int seq_no,
2088 struct cYAML **show_rc, struct cYAML **err_rc)
2091 * TODO: This function is changing in a future patch to accommodate
2092 * PEER_LIST and proper filtering on any nid of the peer
2094 struct lnet_ioctl_peer_cfg peer_info;
2095 struct lnet_peer_ni_credit_info *lpni_cri;
2096 struct lnet_ioctl_element_stats *lpni_stats;
2097 int rc = LUSTRE_CFG_RC_OUT_OF_MEM, ncpt = 0, i = 0, j = 0;
2099 struct cYAML *root = NULL, *peer = NULL, *peer_ni = NULL,
2100 *first_seq = NULL, *peer_root = NULL, *tmp = NULL;
2101 char err_str[LNET_MAX_STR_LEN];
2102 lnet_nid_t prev_primary_nid = LNET_NID_ANY, primary_nid = LNET_NID_ANY;
2103 int data_size = sizeof(*lpni_cri) + sizeof(*lpni_stats);
2104 char *data = malloc(data_size);
2105 bool new_peer = true;
2107 snprintf(err_str, sizeof(err_str),
2108 "\"out of memory\"");
2113 /* create struct cYAML root object */
2114 root = cYAML_create_object(NULL, NULL);
2118 peer_root = cYAML_create_seq(root, "peer");
2119 if (peer_root == NULL)
2123 primary_nid = libcfs_str2nid(knid);
2127 memset(data, 0, data_size);
2128 memset(&peer_info, 0, sizeof(peer_info));
2129 LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2130 peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2131 peer_info.prcfg_count = i;
2132 peer_info.prcfg_bulk = (void *)data;
2133 peer_info.prcfg_size = data_size;
2135 rc = l_ioctl(LNET_DEV_ID,
2136 IOC_LIBCFS_GET_PEER_NI, &peer_info);
2142 if (primary_nid != LNET_NID_ANY &&
2143 primary_nid != peer_info.prcfg_prim_nid)
2146 lpni_cri = peer_info.prcfg_bulk;
2147 lpni_stats = peer_info.prcfg_bulk + sizeof(*lpni_cri);
2149 peer = cYAML_create_seq_item(peer_root);
2153 if (peer_info.prcfg_prim_nid != prev_primary_nid) {
2154 prev_primary_nid = peer_info.prcfg_prim_nid;
2159 lnet_nid_t pnid = peer_info.prcfg_prim_nid;
2160 if (cYAML_create_string(peer, "primary nid",
2161 libcfs_nid2str(pnid))
2164 if (cYAML_create_string(peer, "Multi-Rail",
2165 peer_info.prcfg_mr ?
2169 tmp = cYAML_create_seq(peer, "peer ni");
2175 if (first_seq == NULL)
2178 peer_ni = cYAML_create_seq_item(tmp);
2179 if (peer_ni == NULL)
2182 if (cYAML_create_string(peer_ni, "nid",
2184 (peer_info.prcfg_cfg_nid))
2188 if (cYAML_create_string(peer_ni, "state",
2189 lpni_cri->cr_aliveness)
2196 if (cYAML_create_number(peer_ni, "max_ni_tx_credits",
2197 lpni_cri->cr_ni_peer_tx_credits)
2201 if (cYAML_create_number(peer_ni, "available_tx_credits",
2202 lpni_cri->cr_peer_tx_credits)
2206 if (cYAML_create_number(peer_ni, "min_tx_credits",
2207 lpni_cri->cr_peer_min_tx_credits)
2211 if (cYAML_create_number(peer_ni, "tx_q_num_of_buf",
2212 lpni_cri->cr_peer_tx_qnob)
2216 if (cYAML_create_number(peer_ni, "available_rtr_credits",
2217 lpni_cri->cr_peer_rtr_credits)
2221 if (cYAML_create_number(peer_ni, "min_rtr_credits",
2222 lpni_cri->cr_peer_min_rtr_credits)
2226 if (cYAML_create_number(peer_ni, "send_count",
2227 lpni_stats->iel_send_count)
2231 if (cYAML_create_number(peer_ni, "recv_count",
2232 lpni_stats->iel_recv_count)
2236 if (cYAML_create_number(peer_ni, "drop_count",
2237 lpni_stats->iel_drop_count)
2241 if (cYAML_create_number(peer_ni, "refcount",
2242 lpni_cri->cr_refcount) == NULL)
2246 if (l_errno != ENOENT) {
2249 "\"cannot get peer information: %s\"",
2258 /* print output iff show_rc is not provided */
2259 if (show_rc == NULL)
2260 cYAML_print_tree(root);
2262 snprintf(err_str, sizeof(err_str), "\"success\"");
2263 rc = LUSTRE_CFG_RC_NO_ERR;
2266 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2267 cYAML_free_tree(root);
2268 } else if (show_rc != NULL && *show_rc != NULL) {
2269 struct cYAML *show_node;
2270 /* find the peer node, if one doesn't exist then
2271 * insert one. Otherwise add to the one there
2273 show_node = cYAML_get_object_item(*show_rc,
2275 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2276 cYAML_insert_child(show_node, first_seq);
2279 } else if (show_node == NULL) {
2280 cYAML_insert_sibling((*show_rc)->cy_child,
2284 cYAML_free_tree(root);
2290 cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
2296 static void add_to_global(struct cYAML *show_rc, struct cYAML *node,
2299 struct cYAML *show_node;
2301 show_node = cYAML_get_object_item(show_rc, "global");
2302 if (show_node != NULL)
2303 cYAML_insert_sibling(show_node->cy_child,
2306 cYAML_insert_sibling(show_rc->cy_child,
2311 int lustre_lnet_show_max_intf(int seq_no, struct cYAML **show_rc,
2312 struct cYAML **err_rc)
2314 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2315 char val[LNET_MAX_STR_LEN];
2317 char err_str[LNET_MAX_STR_LEN];
2318 struct cYAML *root = NULL, *global = NULL;
2320 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2322 rc = read_sysfs_file(modparam_path, "lnet_interfaces_max", val,
2323 1, LNET_MAX_STR_LEN);
2325 snprintf(err_str, sizeof(err_str),
2326 "\"cannot get max interfaces: %d\"", rc);
2330 max_intf = atoi(val);
2332 root = cYAML_create_object(NULL, NULL);
2336 global = cYAML_create_object(root, "global");
2340 if (cYAML_create_number(global, "max_intf",
2344 if (show_rc == NULL)
2345 cYAML_print_tree(root);
2347 snprintf(err_str, sizeof(err_str), "\"success\"");
2349 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR)
2350 cYAML_free_tree(root);
2351 else if (show_rc != NULL && *show_rc != NULL)
2352 add_to_global(*show_rc, global, root);
2356 cYAML_build_error(rc, seq_no, SHOW_CMD, "global", err_str, err_rc);
2361 int lustre_lnet_show_numa_range(int seq_no, struct cYAML **show_rc,
2362 struct cYAML **err_rc)
2364 struct lnet_ioctl_set_value data;
2367 char err_str[LNET_MAX_STR_LEN];
2368 struct cYAML *root = NULL, *global = NULL;
2370 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2372 LIBCFS_IOC_INIT_V2(data, sv_hdr);
2374 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NUMA_RANGE, &data);
2379 "\"cannot get numa range: %s\"",
2385 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2387 root = cYAML_create_object(NULL, NULL);
2391 global = cYAML_create_object(root, "global");
2395 if (cYAML_create_number(global, "numa_range",
2396 data.sv_value) == NULL)
2399 if (show_rc == NULL)
2400 cYAML_print_tree(root);
2402 snprintf(err_str, sizeof(err_str), "\"success\"");
2403 rc = LUSTRE_CFG_RC_NO_ERR;
2405 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2406 cYAML_free_tree(root);
2407 } else if (show_rc != NULL && *show_rc != NULL) {
2408 add_to_global(*show_rc, global, root);
2413 cYAML_build_error(rc, seq_no, SHOW_CMD, "global", err_str, err_rc);
2418 int lustre_lnet_show_stats(int seq_no, struct cYAML **show_rc,
2419 struct cYAML **err_rc)
2421 struct lnet_ioctl_lnet_stats data;
2424 char err_str[LNET_MAX_STR_LEN];
2425 struct cYAML *root = NULL, *stats = NULL;
2427 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2429 LIBCFS_IOC_INIT_V2(data, st_hdr);
2431 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LNET_STATS, &data);
2436 "\"cannot get lnet statistics: %s\"",
2442 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2444 root = cYAML_create_object(NULL, NULL);
2448 stats = cYAML_create_object(root, "statistics");
2452 if (cYAML_create_number(stats, "msgs_alloc",
2453 data.st_cntrs.msgs_alloc) == NULL)
2456 if (cYAML_create_number(stats, "msgs_max",
2457 data.st_cntrs.msgs_max) == NULL)
2460 if (cYAML_create_number(stats, "errors",
2461 data.st_cntrs.errors) == NULL)
2464 if (cYAML_create_number(stats, "send_count",
2465 data.st_cntrs.send_count) == NULL)
2468 if (cYAML_create_number(stats, "recv_count",
2469 data.st_cntrs.recv_count) == NULL)
2472 if (cYAML_create_number(stats, "route_count",
2473 data.st_cntrs.route_count) == NULL)
2476 if (cYAML_create_number(stats, "drop_count",
2477 data.st_cntrs.drop_count) == NULL)
2480 if (cYAML_create_number(stats, "send_length",
2481 data.st_cntrs.send_length) == NULL)
2484 if (cYAML_create_number(stats, "recv_length",
2485 data.st_cntrs.recv_length) == NULL)
2488 if (cYAML_create_number(stats, "route_length",
2489 data.st_cntrs.route_length) == NULL)
2492 if (cYAML_create_number(stats, "drop_length",
2493 data.st_cntrs.drop_length) == NULL)
2496 if (show_rc == NULL)
2497 cYAML_print_tree(root);
2499 snprintf(err_str, sizeof(err_str), "\"success\"");
2500 rc = LUSTRE_CFG_RC_NO_ERR;
2502 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2503 cYAML_free_tree(root);
2504 } else if (show_rc != NULL && *show_rc != NULL) {
2505 cYAML_insert_sibling((*show_rc)->cy_child,
2512 cYAML_build_error(rc, seq_no, SHOW_CMD, "statistics", err_str, err_rc);
2517 typedef int (*cmd_handler_t)(struct cYAML *tree,
2518 struct cYAML **show_rc,
2519 struct cYAML **err_rc);
2521 static int handle_yaml_config_route(struct cYAML *tree, struct cYAML **show_rc,
2522 struct cYAML **err_rc)
2524 struct cYAML *net, *gw, *hop, *prio, *seq_no;
2526 net = cYAML_get_object_item(tree, "net");
2527 gw = cYAML_get_object_item(tree, "gateway");
2528 hop = cYAML_get_object_item(tree, "hop");
2529 prio = cYAML_get_object_item(tree, "priority");
2530 seq_no = cYAML_get_object_item(tree, "seq_no");
2532 return lustre_lnet_config_route((net) ? net->cy_valuestring : NULL,
2533 (gw) ? gw->cy_valuestring : NULL,
2534 (hop) ? hop->cy_valueint : -1,
2535 (prio) ? prio->cy_valueint : -1,
2536 (seq_no) ? seq_no->cy_valueint : -1,
2540 static void yaml_free_string_array(char **array, int num)
2543 char **sub_array = array;
2545 for (i = 0; i < num; i++) {
2546 if (*sub_array != NULL)
2556 * 0: <intf_name>['['<expr>']']
2557 * 1: <intf_name>['['<expr>']']
2559 static int yaml_copy_intf_info(struct cYAML *intf_tree,
2560 struct lnet_dlc_network_descr *nw_descr)
2562 struct cYAML *child = NULL;
2563 int intf_num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
2564 struct lnet_dlc_intf_descr *intf_descr, *tmp;
2566 if (intf_tree == NULL || nw_descr == NULL)
2567 return LUSTRE_CFG_RC_BAD_PARAM;
2569 /* now grab all the interfaces and their cpts */
2570 child = intf_tree->cy_child;
2571 while (child != NULL) {
2572 if (child->cy_valuestring == NULL) {
2573 child = child->cy_next;
2577 if (strlen(child->cy_valuestring) >= LNET_MAX_STR_LEN)
2580 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist,
2581 child->cy_valuestring,
2582 strlen(child->cy_valuestring));
2583 if (rc != LUSTRE_CFG_RC_NO_ERR)
2587 child = child->cy_next;
2591 return LUSTRE_CFG_RC_MISSING_PARAM;
2596 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
2598 list_del(&intf_descr->intf_on_network);
2599 free_intf_descr(intf_descr);
2606 yaml_extract_cmn_tunables(struct cYAML *tree,
2607 struct lnet_ioctl_config_lnd_cmn_tunables *tunables,
2608 struct cfs_expr_list **global_cpts)
2610 struct cYAML *tun, *item, *smp;
2613 tun = cYAML_get_object_item(tree, "tunables");
2615 item = cYAML_get_object_item(tun, "peer_timeout");
2617 tunables->lct_peer_timeout = item->cy_valueint;
2618 item = cYAML_get_object_item(tun, "peer_credits");
2620 tunables->lct_peer_tx_credits = item->cy_valueint;
2621 item = cYAML_get_object_item(tun, "peer_buffer_credits");
2623 tunables->lct_peer_rtr_credits = item->cy_valueint;
2624 item = cYAML_get_object_item(tun, "credits");
2626 tunables->lct_max_tx_credits = item->cy_valueint;
2627 smp = cYAML_get_object_item(tun, "CPT");
2629 rc = cfs_expr_list_parse(smp->cy_valuestring,
2630 strlen(smp->cy_valuestring),
2631 0, UINT_MAX, global_cpts);
2633 *global_cpts = NULL;
2643 yaml_extract_tunables(struct cYAML *tree,
2644 struct lnet_ioctl_config_lnd_tunables *tunables,
2645 struct cfs_expr_list **global_cpts,
2650 rc = yaml_extract_cmn_tunables(tree, &tunables->lt_cmn,
2656 lustre_yaml_extract_lnd_tunables(tree, net_type,
2664 * - net type: <net>[<NUM>]
2666 * - nid: <ip>@<net>[<NUM>]
2669 * 0: <intf_name>['['<expr>']']
2670 * 1: <intf_name>['['<expr>']']
2672 * peer_timeout: <NUM>
2673 * peer_credits: <NUM>
2674 * peer_buffer_credits: <NUM>
2677 * peercredits_hiw: <NUM>
2678 * map_on_demand: <NUM>
2679 * concurrent_sends: <NUM>
2680 * fmr_pool_size: <NUM>
2681 * fmr_flush_trigger: <NUM>
2684 * At least one interface is required. If no interfaces are provided the
2685 * network interface can not be configured.
2687 static int handle_yaml_config_ni(struct cYAML *tree, struct cYAML **show_rc,
2688 struct cYAML **err_rc)
2690 struct cYAML *net, *intf, *seq_no, *ip2net = NULL, *local_nis = NULL,
2692 int num_entries = 0, rc;
2693 struct lnet_dlc_network_descr nw_descr;
2694 struct cfs_expr_list *global_cpts = NULL;
2695 struct lnet_ioctl_config_lnd_tunables tunables;
2698 memset(&tunables, 0, sizeof(tunables));
2700 INIT_LIST_HEAD(&nw_descr.network_on_rule);
2701 INIT_LIST_HEAD(&nw_descr.nw_intflist);
2703 ip2net = cYAML_get_object_item(tree, "ip2net");
2704 net = cYAML_get_object_item(tree, "net type");
2706 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
2708 nw_descr.nw_id = LOLND;
2711 * if neither net nor ip2nets are present, then we can not
2712 * configure the network.
2714 if (!net && !ip2net)
2715 return LUSTRE_CFG_RC_MISSING_PARAM;
2717 local_nis = cYAML_get_object_item(tree, "local NI(s)");
2718 if (local_nis == NULL)
2719 return LUSTRE_CFG_RC_MISSING_PARAM;
2721 if (!cYAML_is_sequence(local_nis))
2722 return LUSTRE_CFG_RC_BAD_PARAM;
2724 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
2725 intf = cYAML_get_object_item(item, "interfaces");
2728 num_entries = yaml_copy_intf_info(intf, &nw_descr);
2729 if (num_entries <= 0) {
2730 cYAML_build_error(num_entries, -1, "ni", "add",
2731 "bad interface list",
2733 return LUSTRE_CFG_RC_BAD_PARAM;
2737 found = yaml_extract_tunables(tree, &tunables, &global_cpts,
2738 LNET_NETTYP(nw_descr.nw_id));
2739 seq_no = cYAML_get_object_item(tree, "seq_no");
2741 rc = lustre_lnet_config_ni(&nw_descr,
2743 (ip2net) ? ip2net->cy_valuestring : NULL,
2744 (found) ? &tunables: NULL,
2745 (seq_no) ? seq_no->cy_valueint : -1,
2748 if (global_cpts != NULL)
2749 cfs_expr_list_free(global_cpts);
2756 * - net-spec: <tcp|o2ib|gni>[NUM]
2758 * 0: <intf name>['['<expr>']']
2759 * 1: <intf name>['['<expr>']']
2761 * 0: <expr.expr.expr.expr>
2762 * 1: <expr.expr.expr.expr>
2764 static int handle_yaml_config_ip2nets(struct cYAML *tree,
2765 struct cYAML **show_rc,
2766 struct cYAML **err_rc)
2768 struct cYAML *net, *ip_range, *item = NULL, *intf = NULL,
2770 struct lustre_lnet_ip2nets ip2nets;
2771 struct lustre_lnet_ip_range_descr *ip_range_descr = NULL,
2773 int rc = LUSTRE_CFG_RC_NO_ERR;
2774 struct cfs_expr_list *global_cpts = NULL;
2775 struct cfs_expr_list *el, *el_tmp;
2776 struct lnet_ioctl_config_lnd_tunables tunables;
2777 struct lnet_dlc_intf_descr *intf_descr, *intf_tmp;
2780 memset(&tunables, 0, sizeof(tunables));
2782 /* initialize all lists */
2783 INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
2784 INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
2785 INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
2787 net = cYAML_get_object_item(tree, "net-spec");
2789 return LUSTRE_CFG_RC_BAD_PARAM;
2791 if (net != NULL && net->cy_valuestring == NULL)
2792 return LUSTRE_CFG_RC_BAD_PARAM;
2794 /* assign the network id */
2795 ip2nets.ip2nets_net.nw_id = libcfs_str2net(net->cy_valuestring);
2796 if (ip2nets.ip2nets_net.nw_id == LNET_NID_ANY)
2797 return LUSTRE_CFG_RC_BAD_PARAM;
2799 seq_no = cYAML_get_object_item(tree, "seq_no");
2801 intf = cYAML_get_object_item(tree, "interfaces");
2803 rc = yaml_copy_intf_info(intf, &ip2nets.ip2nets_net);
2805 return LUSTRE_CFG_RC_BAD_PARAM;
2808 ip_range = cYAML_get_object_item(tree, "ip-range");
2809 if (ip_range != NULL) {
2810 item = ip_range->cy_child;
2811 while (item != NULL) {
2812 if (item->cy_valuestring == NULL) {
2813 item = item->cy_next;
2817 rc = lustre_lnet_add_ip_range(&ip2nets.ip2nets_ip_ranges,
2818 item->cy_valuestring);
2820 if (rc != LUSTRE_CFG_RC_NO_ERR)
2823 item = item->cy_next;
2827 found = yaml_extract_tunables(tree, &tunables, &global_cpts,
2828 LNET_NETTYP(ip2nets.ip2nets_net.nw_id));
2830 rc = lustre_lnet_config_ip2nets(&ip2nets,
2831 (found) ? &tunables : NULL,
2833 (seq_no) ? seq_no->cy_valueint : -1,
2837 * don't stop because there was no match. Continue processing the
2838 * rest of the rules. If non-match then nothing is configured
2840 if (rc == LUSTRE_CFG_RC_NO_MATCH)
2841 rc = LUSTRE_CFG_RC_NO_ERR;
2843 list_for_each_entry_safe(intf_descr, intf_tmp,
2844 &ip2nets.ip2nets_net.nw_intflist,
2846 list_del(&intf_descr->intf_on_network);
2847 free_intf_descr(intf_descr);
2850 list_for_each_entry_safe(ip_range_descr, tmp,
2851 &ip2nets.ip2nets_ip_ranges,
2853 list_del(&ip_range_descr->ipr_entry);
2854 list_for_each_entry_safe(el, el_tmp, &ip_range_descr->ipr_expr,
2856 list_del(&el->el_link);
2857 cfs_expr_list_free(el);
2859 free(ip_range_descr);
2865 static int handle_yaml_del_ni(struct cYAML *tree, struct cYAML **show_rc,
2866 struct cYAML **err_rc)
2868 struct cYAML *net = NULL, *intf = NULL, *seq_no = NULL, *item = NULL,
2870 int num_entries, rc;
2871 struct lnet_dlc_network_descr nw_descr;
2873 INIT_LIST_HEAD(&nw_descr.network_on_rule);
2874 INIT_LIST_HEAD(&nw_descr.nw_intflist);
2876 net = cYAML_get_object_item(tree, "net type");
2878 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
2880 local_nis = cYAML_get_object_item(tree, "local NI(s)");
2881 if (local_nis == NULL)
2882 return LUSTRE_CFG_RC_MISSING_PARAM;
2884 if (!cYAML_is_sequence(local_nis))
2885 return LUSTRE_CFG_RC_BAD_PARAM;
2887 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
2888 intf = cYAML_get_object_item(item, "interfaces");
2891 num_entries = yaml_copy_intf_info(intf, &nw_descr);
2892 if (num_entries <= 0) {
2893 cYAML_build_error(num_entries, -1, "ni", "add",
2894 "bad interface list",
2896 return LUSTRE_CFG_RC_BAD_PARAM;
2900 seq_no = cYAML_get_object_item(tree, "seq_no");
2902 rc = lustre_lnet_del_ni((net) ? &nw_descr : NULL,
2903 (seq_no) ? seq_no->cy_valueint : -1,
2909 static int yaml_copy_peer_nids(struct cYAML *tree, char ***nidsppp, bool del)
2911 struct cYAML *nids_entry = NULL, *child = NULL, *entry = NULL,
2914 int num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
2916 prim_nid = cYAML_get_object_item(tree, "primary nid");
2917 if (!prim_nid || !prim_nid->cy_valuestring)
2918 return LUSTRE_CFG_RC_MISSING_PARAM;
2920 nids_entry = cYAML_get_object_item(tree, "peer ni");
2921 if (cYAML_is_sequence(nids_entry)) {
2922 while (cYAML_get_next_seq_item(nids_entry, &child)) {
2923 entry = cYAML_get_object_item(child, "nid");
2924 /* don't count an empty entry */
2925 if (!entry || !entry->cy_valuestring)
2928 if ((strcmp(entry->cy_valuestring, prim_nid->cy_valuestring)
2931 * primary nid is present in the list of
2932 * nids so that means we want to delete
2933 * the entire peer, so no need to go
2934 * further. Just delete the entire peer.
2944 return LUSTRE_CFG_RC_MISSING_PARAM;
2946 nids = calloc(sizeof(*nids) * num, 1);
2948 return LUSTRE_CFG_RC_OUT_OF_MEM;
2950 /* now grab all the nids */
2953 while (cYAML_get_next_seq_item(nids_entry, &child)) {
2954 entry = cYAML_get_object_item(child, "nid");
2955 if (!entry || !entry->cy_valuestring)
2958 nids[num] = calloc(strlen(entry->cy_valuestring) + 1, 1);
2960 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2963 strncpy(nids[num], entry->cy_valuestring,
2964 strlen(entry->cy_valuestring));
2974 yaml_free_string_array(nids, num);
2979 static int handle_yaml_config_peer(struct cYAML *tree, struct cYAML **show_rc,
2980 struct cYAML **err_rc)
2984 struct cYAML *seq_no, *prim_nid, *non_mr;
2986 num = yaml_copy_peer_nids(tree, &nids, false);
2990 seq_no = cYAML_get_object_item(tree, "seq_no");
2991 prim_nid = cYAML_get_object_item(tree, "primary nid");
2992 non_mr = cYAML_get_object_item(tree, "non_mr");
2994 rc = lustre_lnet_config_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
2996 (non_mr) ? false : true,
2997 (seq_no) ? seq_no->cy_valueint : -1,
3000 yaml_free_string_array(nids, num);
3004 static int handle_yaml_del_peer(struct cYAML *tree, struct cYAML **show_rc,
3005 struct cYAML **err_rc)
3009 struct cYAML *seq_no, *prim_nid;
3011 num = yaml_copy_peer_nids(tree, &nids, true);
3015 seq_no = cYAML_get_object_item(tree, "seq_no");
3016 prim_nid = cYAML_get_object_item(tree, "primary nid");
3018 rc = lustre_lnet_del_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
3020 (seq_no) ? seq_no->cy_valueint : -1,
3023 yaml_free_string_array(nids, num);
3027 static int handle_yaml_config_buffers(struct cYAML *tree,
3028 struct cYAML **show_rc,
3029 struct cYAML **err_rc)
3032 struct cYAML *tiny, *small, *large, *seq_no;
3034 tiny = cYAML_get_object_item(tree, "tiny");
3035 small = cYAML_get_object_item(tree, "small");
3036 large = cYAML_get_object_item(tree, "large");
3037 seq_no = cYAML_get_object_item(tree, "seq_no");
3039 rc = lustre_lnet_config_buffers((tiny) ? tiny->cy_valueint : -1,
3040 (small) ? small->cy_valueint : -1,
3041 (large) ? large->cy_valueint : -1,
3042 (seq_no) ? seq_no->cy_valueint : -1,
3048 static int handle_yaml_config_routing(struct cYAML *tree,
3049 struct cYAML **show_rc,
3050 struct cYAML **err_rc)
3052 int rc = LUSTRE_CFG_RC_NO_ERR;
3053 struct cYAML *seq_no, *enable;
3055 seq_no = cYAML_get_object_item(tree, "seq_no");
3056 enable = cYAML_get_object_item(tree, "enable");
3059 rc = lustre_lnet_enable_routing(enable->cy_valueint,
3061 seq_no->cy_valueint : -1,
3068 static int handle_yaml_del_route(struct cYAML *tree, struct cYAML **show_rc,
3069 struct cYAML **err_rc)
3073 struct cYAML *seq_no;
3075 net = cYAML_get_object_item(tree, "net");
3076 gw = cYAML_get_object_item(tree, "gateway");
3077 seq_no = cYAML_get_object_item(tree, "seq_no");
3079 return lustre_lnet_del_route((net) ? net->cy_valuestring : NULL,
3080 (gw) ? gw->cy_valuestring : NULL,
3081 (seq_no) ? seq_no->cy_valueint : -1,
3085 static int handle_yaml_del_routing(struct cYAML *tree, struct cYAML **show_rc,
3086 struct cYAML **err_rc)
3088 struct cYAML *seq_no;
3090 seq_no = cYAML_get_object_item(tree, "seq_no");
3092 return lustre_lnet_enable_routing(0, (seq_no) ?
3093 seq_no->cy_valueint : -1,
3097 static int handle_yaml_show_route(struct cYAML *tree, struct cYAML **show_rc,
3098 struct cYAML **err_rc)
3104 struct cYAML *detail;
3105 struct cYAML *seq_no;
3107 net = cYAML_get_object_item(tree, "net");
3108 gw = cYAML_get_object_item(tree, "gateway");
3109 hop = cYAML_get_object_item(tree, "hop");
3110 prio = cYAML_get_object_item(tree, "priority");
3111 detail = cYAML_get_object_item(tree, "detail");
3112 seq_no = cYAML_get_object_item(tree, "seq_no");
3114 return lustre_lnet_show_route((net) ? net->cy_valuestring : NULL,
3115 (gw) ? gw->cy_valuestring : NULL,
3116 (hop) ? hop->cy_valueint : -1,
3117 (prio) ? prio->cy_valueint : -1,
3118 (detail) ? detail->cy_valueint : 0,
3119 (seq_no) ? seq_no->cy_valueint : -1,
3124 static int handle_yaml_show_net(struct cYAML *tree, struct cYAML **show_rc,
3125 struct cYAML **err_rc)
3127 struct cYAML *net, *detail, *seq_no;
3129 net = cYAML_get_object_item(tree, "net");
3130 detail = cYAML_get_object_item(tree, "detail");
3131 seq_no = cYAML_get_object_item(tree, "seq_no");
3133 return lustre_lnet_show_net((net) ? net->cy_valuestring : NULL,
3134 (detail) ? detail->cy_valueint : 0,
3135 (seq_no) ? seq_no->cy_valueint : -1,
3140 static int handle_yaml_show_routing(struct cYAML *tree, struct cYAML **show_rc,
3141 struct cYAML **err_rc)
3143 struct cYAML *seq_no;
3145 seq_no = cYAML_get_object_item(tree, "seq_no");
3147 return lustre_lnet_show_routing((seq_no) ? seq_no->cy_valueint : -1,
3151 static int handle_yaml_show_peers(struct cYAML *tree, struct cYAML **show_rc,
3152 struct cYAML **err_rc)
3154 struct cYAML *seq_no, *nid, *detail;
3156 seq_no = cYAML_get_object_item(tree, "seq_no");
3157 detail = cYAML_get_object_item(tree, "detail");
3158 nid = cYAML_get_object_item(tree, "nid");
3160 return lustre_lnet_show_peer((nid) ? nid->cy_valuestring : NULL,
3161 (detail) ? detail->cy_valueint : 0,
3162 (seq_no) ? seq_no->cy_valueint : -1,
3166 static int handle_yaml_show_stats(struct cYAML *tree, struct cYAML **show_rc,
3167 struct cYAML **err_rc)
3169 struct cYAML *seq_no;
3171 seq_no = cYAML_get_object_item(tree, "seq_no");
3173 return lustre_lnet_show_stats((seq_no) ? seq_no->cy_valueint : -1,
3177 static int handle_yaml_config_global_settings(struct cYAML *tree,
3178 struct cYAML **show_rc,
3179 struct cYAML **err_rc)
3181 struct cYAML *max_intf, *numa, *seq_no;
3184 seq_no = cYAML_get_object_item(tree, "seq_no");
3185 max_intf = cYAML_get_object_item(tree, "max_intf");
3187 rc = lustre_lnet_config_max_intf(max_intf->cy_valueint,
3188 seq_no ? seq_no->cy_valueint
3192 numa = cYAML_get_object_item(tree, "numa_range");
3194 rc = lustre_lnet_config_numa_range(numa->cy_valueint,
3195 seq_no ? seq_no->cy_valueint
3202 static int handle_yaml_del_global_settings(struct cYAML *tree,
3203 struct cYAML **show_rc,
3204 struct cYAML **err_rc)
3206 struct cYAML *max_intf, *numa, *seq_no;
3209 seq_no = cYAML_get_object_item(tree, "seq_no");
3210 max_intf = cYAML_get_object_item(tree, "max_intf");
3212 rc = lustre_lnet_config_max_intf(LNET_INTERFACES_MAX_DEFAULT,
3213 seq_no ? seq_no->cy_valueint
3217 numa = cYAML_get_object_item(tree, "numa_range");
3219 rc = lustre_lnet_config_numa_range(0,
3220 seq_no ? seq_no->cy_valueint
3227 static int handle_yaml_show_global_settings(struct cYAML *tree,
3228 struct cYAML **show_rc,
3229 struct cYAML **err_rc)
3231 struct cYAML *max_intf, *numa, *seq_no;
3234 seq_no = cYAML_get_object_item(tree, "seq_no");
3235 max_intf = cYAML_get_object_item(tree, "max_intf");
3237 rc = lustre_lnet_show_max_intf(seq_no ? seq_no->cy_valueint
3241 numa = cYAML_get_object_item(tree, "numa_range");
3243 rc = lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint
3250 struct lookup_cmd_hdlr_tbl {
3255 static struct lookup_cmd_hdlr_tbl lookup_config_tbl[] = {
3256 { .name = "route", .cb = handle_yaml_config_route },
3257 { .name = "net", .cb = handle_yaml_config_ni },
3258 { .name = "ip2nets", .cb = handle_yaml_config_ip2nets },
3259 { .name = "peer", .cb = handle_yaml_config_peer },
3260 { .name = "routing", .cb = handle_yaml_config_routing },
3261 { .name = "buffers", .cb = handle_yaml_config_buffers },
3262 { .name = "global", .cb = handle_yaml_config_global_settings},
3265 static struct lookup_cmd_hdlr_tbl lookup_del_tbl[] = {
3266 { .name = "route", .cb = handle_yaml_del_route },
3267 { .name = "net", .cb = handle_yaml_del_ni },
3268 { .name = "peer", .cb = handle_yaml_del_peer },
3269 { .name = "routing", .cb = handle_yaml_del_routing },
3270 { .name = "global", .cb = handle_yaml_del_global_settings},
3273 static struct lookup_cmd_hdlr_tbl lookup_show_tbl[] = {
3274 { .name = "route", .cb = handle_yaml_show_route },
3275 { .name = "net", .cb = handle_yaml_show_net },
3276 { .name = "buffers", .cb = handle_yaml_show_routing },
3277 { .name = "routing", .cb = handle_yaml_show_routing },
3278 { .name = "peer", .cb = handle_yaml_show_peers },
3279 { .name = "statistics", .cb = handle_yaml_show_stats },
3280 { .name = "global", .cb = handle_yaml_show_global_settings},
3283 static cmd_handler_t lookup_fn(char *key,
3284 struct lookup_cmd_hdlr_tbl *tbl)
3290 for (i = 0; tbl[i].name != NULL; i++) {
3291 if (strncmp(key, tbl[i].name, strlen(tbl[i].name)) == 0)
3298 static int lustre_yaml_cb_helper(char *f, struct lookup_cmd_hdlr_tbl *table,
3299 struct cYAML **show_rc, struct cYAML **err_rc)
3301 struct cYAML *tree, *item = NULL, *head, *child;
3303 char err_str[LNET_MAX_STR_LEN];
3304 int rc = LUSTRE_CFG_RC_NO_ERR, return_rc = LUSTRE_CFG_RC_NO_ERR;
3306 tree = cYAML_build_tree(f, NULL, 0, err_rc, false);
3308 return LUSTRE_CFG_RC_BAD_PARAM;
3310 child = tree->cy_child;
3311 while (child != NULL) {
3312 cb = lookup_fn(child->cy_string, table);
3314 snprintf(err_str, sizeof(err_str),
3315 "\"call back for '%s' not found\"",
3317 cYAML_build_error(LUSTRE_CFG_RC_BAD_PARAM, -1,
3318 "yaml", "helper", err_str, err_rc);
3322 if (cYAML_is_sequence(child)) {
3323 while ((head = cYAML_get_next_seq_item(child, &item))
3325 rc = cb(head, show_rc, err_rc);
3326 if (rc != LUSTRE_CFG_RC_NO_ERR)
3330 rc = cb(child, show_rc, err_rc);
3331 if (rc != LUSTRE_CFG_RC_NO_ERR)
3335 child = child->cy_next;
3339 cYAML_free_tree(tree);
3344 int lustre_yaml_config(char *f, struct cYAML **err_rc)
3346 return lustre_yaml_cb_helper(f, lookup_config_tbl,
3350 int lustre_yaml_del(char *f, struct cYAML **err_rc)
3352 return lustre_yaml_cb_helper(f, lookup_del_tbl,
3356 int lustre_yaml_show(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
3358 return lustre_yaml_cb_helper(f, lookup_show_tbl,