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 <lnet/lnetctl.h>
48 #include <lnet/socklnd.h>
50 #include <lnet/lnet.h>
51 #include <sys/types.h>
53 #include "liblnetconfig.h"
56 #define CONFIG_CMD "configure"
57 #define UNCONFIG_CMD "unconfigure"
60 #define SHOW_CMD "show"
64 * lustre_lnet_ip_range_descr
65 * Describes an IP range.
66 * Each octect is an expression
68 struct lustre_lnet_ip_range_descr {
69 struct list_head ipr_entry;
70 struct list_head ipr_expr;
75 * Describes an ip2nets rule. This can be on a list of rules.
77 struct lustre_lnet_ip2nets {
78 struct lnet_dlc_network_descr ip2nets_net;
79 struct list_head ip2nets_ip_ranges;
84 * frees the memory allocated for an intf descriptor.
86 void free_intf_descr(struct lnet_dlc_intf_descr *intf_descr)
91 if (intf_descr->cpt_expr != NULL)
92 cfs_expr_list_free(intf_descr->cpt_expr);
97 * lustre_lnet_add_ip_range
99 * given a string of the format:
100 * <expr.expr.expr.expr> parse each expr into
101 * a lustre_lnet_ip_range_descr structure and insert on the list.
103 * This function is called from
104 * YAML on each ip-range.
105 * As a result of lnetctl command
106 * When building a NID or P2P selection rules
108 int lustre_lnet_add_ip_range(struct list_head *list, char *str_ip_range)
110 struct lustre_lnet_ip_range_descr *ip_range;
113 ip_range = calloc(1, sizeof(*ip_range));
114 if (ip_range == NULL)
115 return LUSTRE_CFG_RC_OUT_OF_MEM;
117 INIT_LIST_HEAD(&ip_range->ipr_entry);
118 INIT_LIST_HEAD(&ip_range->ipr_expr);
120 rc = cfs_ip_addr_parse(str_ip_range, strlen(str_ip_range),
121 &ip_range->ipr_expr);
123 return LUSTRE_CFG_RC_BAD_PARAM;
125 list_add_tail(&ip_range->ipr_entry, list);
127 return LUSTRE_CFG_RC_NO_ERR;
130 int lustre_lnet_add_intf_descr(struct list_head *list, char *intf, int len)
132 char *open_sq_bracket = NULL, *close_sq_bracket = NULL,
134 struct lnet_dlc_intf_descr *intf_descr = NULL;
136 char intf_string[LNET_MAX_STR_LEN];
138 if (len >= LNET_MAX_STR_LEN)
139 return LUSTRE_CFG_RC_BAD_PARAM;
141 strncpy(intf_string, intf, len);
142 intf_string[len] = '\0';
144 intf_descr = calloc(1, sizeof(*intf_descr));
145 if (intf_descr == NULL)
146 return LUSTRE_CFG_RC_OUT_OF_MEM;
148 INIT_LIST_HEAD(&intf_descr->intf_on_network);
150 intf_name = intf_string;
151 open_sq_bracket = strchr(intf_string, '[');
152 if (open_sq_bracket != NULL) {
153 close_sq_bracket = strchr(intf_string, ']');
154 if (close_sq_bracket == NULL) {
156 return LUSTRE_CFG_RC_BAD_PARAM;
158 rc = cfs_expr_list_parse(open_sq_bracket,
159 strlen(open_sq_bracket), 0, UINT_MAX,
160 &intf_descr->cpt_expr);
163 return LUSTRE_CFG_RC_BAD_PARAM;
165 strncpy(intf_descr->intf_name, intf_name,
166 open_sq_bracket - intf_name);
167 intf_descr->intf_name[open_sq_bracket - intf_name] = '\0';
169 strcpy(intf_descr->intf_name, intf_name);
170 intf_descr->cpt_expr = NULL;
173 list_add_tail(&intf_descr->intf_on_network, list);
175 return LUSTRE_CFG_RC_NO_ERR;
178 void lustre_lnet_init_nw_descr(struct lnet_dlc_network_descr *nw_descr)
180 if (nw_descr != NULL) {
181 INIT_LIST_HEAD(&nw_descr->network_on_rule);
182 INIT_LIST_HEAD(&nw_descr->nw_intflist);
186 int lustre_lnet_parse_nids(char *nids, char **array, int size,
190 char *comma = nids, *cur, *entry;
192 int i, len, start = 0, finish = 0;
194 if (nids == NULL || strlen(nids) == 0)
197 /* count the number or new nids, by counting the number of commas */
199 comma = strchr(comma, ',');
209 * if the array is not NULL allocate a large enough array to house
210 * the old and new entries
212 new_array = calloc(sizeof(char*),
213 (size > 0) ? size + num_nids : num_nids);
218 /* parse our the new nids and add them to the tail of the array */
221 start = (size > 0) ? size: 0;
222 finish = (size > 0) ? size + num_nids : num_nids;
223 for (i = start; i < finish; i++) {
224 comma = strchr(comma, ',');
227 * the length of the string to be parsed out is
228 * from cur to end of string. So it's good enough
231 len = strlen(cur) + 1;
233 /* length of the string is comma - cur */
234 len = (comma - cur) + 1;
236 entry = calloc(1, len);
238 finish = i > 0 ? i - 1: 0;
241 strncpy(entry, cur, len - 1);
243 new_array[i] = entry;
250 /* add the old entries in the array and delete the old array*/
251 for (i = 0; i < size; i++)
252 new_array[i] = array[i];
257 *out_array = new_array;
262 for (i = start; i < finish; i++)
272 * <intf>[<expr>], <intf>[<expr>],..
274 int lustre_lnet_parse_interfaces(char *intf_str,
275 struct lnet_dlc_network_descr *nw_descr)
280 char *cur = intf_str, *next = NULL;
281 char *end = intf_str + strlen(intf_str);
283 struct lnet_dlc_intf_descr *intf_descr, *tmp;
285 if (nw_descr == NULL)
286 return LUSTRE_CFG_RC_BAD_PARAM;
289 open_square = strchr(cur, '[');
290 if (open_square != NULL) {
291 close_square = strchr(cur, ']');
292 if (close_square == NULL) {
293 rc = LUSTRE_CFG_RC_BAD_PARAM;
297 comma = strchr(cur, ',');
298 if (comma != NULL && comma > close_square) {
300 len = next - close_square;
306 comma = strchr(cur, ',');
316 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist, cur, len);
317 if (rc != LUSTRE_CFG_RC_NO_ERR)
323 return LUSTRE_CFG_RC_NO_ERR;
326 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
328 list_del(&intf_descr->intf_on_network);
329 free_intf_descr(intf_descr);
335 int lustre_lnet_config_lib_init(void)
337 return register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH,
338 LNET_DEV_MAJOR, LNET_DEV_MINOR);
341 void lustre_lnet_config_lib_uninit(void)
343 unregister_ioc_dev(LNET_DEV_ID);
346 int lustre_lnet_config_ni_system(bool up, bool load_ni_from_mod,
347 int seq_no, struct cYAML **err_rc)
349 struct libcfs_ioctl_data data;
352 char err_str[LNET_MAX_STR_LEN];
354 snprintf(err_str, sizeof(err_str), "\"Success\"");
356 LIBCFS_IOC_INIT(data);
358 /* Reverse logic is used here in order not to change
359 * the lctl utility */
360 data.ioc_flags = load_ni_from_mod ? 0 : 1;
362 opc = up ? IOC_LIBCFS_CONFIGURE : IOC_LIBCFS_UNCONFIGURE;
364 rc = l_ioctl(LNET_DEV_ID, opc, &data);
368 "\"LNet %s error: %s\"", (up) ? "configure" :
369 "unconfigure", strerror(errno));
373 cYAML_build_error(rc, seq_no, (up) ? CONFIG_CMD : UNCONFIG_CMD,
374 "lnet", err_str, err_rc);
379 static lnet_nid_t *allocate_create_nid_array(char **nids, __u32 num_nids,
382 lnet_nid_t *array = NULL;
386 snprintf(err_str, LNET_MAX_STR_LEN, "no NIDs to add");
390 array = calloc(sizeof(*array) * num_nids, 1);
392 snprintf(err_str, LNET_MAX_STR_LEN, "out of memory");
396 for (i = 0; i < num_nids; i++) {
397 array[i] = libcfs_str2nid(nids[i]);
398 if (array[i] == LNET_NID_ANY) {
400 snprintf(err_str, LNET_MAX_STR_LEN,
410 static int dispatch_peer_ni_cmd(lnet_nid_t pnid, lnet_nid_t nid, __u32 cmd,
411 struct lnet_ioctl_peer_cfg *data,
412 char *err_str, char *cmd_str)
416 data->prcfg_prim_nid = pnid;
417 data->prcfg_cfg_nid = nid;
419 rc = l_ioctl(LNET_DEV_ID, cmd, data);
424 "\"cannot %s peer ni: %s\"",
425 (cmd_str) ? cmd_str : "add", strerror(errno));
431 int lustre_lnet_config_peer_nid(char *pnid, char **nid, int num_nids,
432 bool mr, int seq_no, struct cYAML **err_rc)
434 struct lnet_ioctl_peer_cfg data;
435 lnet_nid_t prim_nid = LNET_NID_ANY;
436 int rc = LUSTRE_CFG_RC_NO_ERR;
438 bool nid0_used = false;
439 char err_str[LNET_MAX_STR_LEN] = {0};
440 lnet_nid_t *nids = allocate_create_nid_array(nid, num_nids, err_str);
443 prim_nid = libcfs_str2nid(pnid);
444 if (prim_nid == LNET_NID_ANY) {
445 snprintf(err_str, sizeof(err_str),
448 rc = LUSTRE_CFG_RC_MISSING_PARAM;
451 } else if (!nids || nids[0] == LNET_NID_ANY) {
452 snprintf(err_str, sizeof(err_str),
453 "no NIDs provided for configuration");
454 rc = LUSTRE_CFG_RC_MISSING_PARAM;
457 prim_nid = LNET_NID_ANY;
460 snprintf(err_str, sizeof(err_str), "\"Success\"");
462 LIBCFS_IOC_INIT_V2(data, prcfg_hdr);
466 * if prim_nid is not specified use the first nid in the list of
467 * nids provided as the prim_nid. NOTE: on entering 'if' we must
468 * have at least 1 NID
470 if (prim_nid == LNET_NID_ANY) {
475 /* Create the prim_nid first */
476 rc = dispatch_peer_ni_cmd(prim_nid, LNET_NID_ANY,
477 IOC_LIBCFS_ADD_PEER_NI,
478 &data, err_str, "add");
483 /* add the rest of the nids to the key nid if any are available */
484 for (idx = nid0_used ? 1 : 0 ; nids && idx < num_nids; idx++) {
486 * If prim_nid is not provided then the first nid in the
487 * list becomes the prim_nid. First time round the loop use
488 * LNET_NID_ANY for the first parameter, then use nid[0]
489 * as the key nid after wards
491 rc = dispatch_peer_ni_cmd(prim_nid, nids[idx],
492 IOC_LIBCFS_ADD_PEER_NI, &data,
502 cYAML_build_error(rc, seq_no, ADD_CMD, "peer_ni", err_str, err_rc);
506 int lustre_lnet_del_peer_nid(char *pnid, char **nid, int num_nids,
507 int seq_no, struct cYAML **err_rc)
509 struct lnet_ioctl_peer_cfg data;
511 int rc = LUSTRE_CFG_RC_NO_ERR;
513 char err_str[LNET_MAX_STR_LEN] = {0};
514 lnet_nid_t *nids = allocate_create_nid_array(nid, num_nids, err_str);
517 snprintf(err_str, sizeof(err_str),
518 "\"Primary nid is not provided\"");
519 rc = LUSTRE_CFG_RC_MISSING_PARAM;
522 prim_nid = libcfs_str2nid(pnid);
523 if (prim_nid == LNET_NID_ANY) {
524 rc = LUSTRE_CFG_RC_BAD_PARAM;
525 snprintf(err_str, sizeof(err_str),
532 snprintf(err_str, sizeof(err_str), "\"Success\"");
534 LIBCFS_IOC_INIT_V2(data, prcfg_hdr);
535 if (!nids || nids[0] == LNET_NID_ANY) {
536 rc = dispatch_peer_ni_cmd(prim_nid, LNET_NID_ANY,
537 IOC_LIBCFS_DEL_PEER_NI,
538 &data, err_str, "del");
542 for (idx = 0; nids && idx < num_nids; idx++) {
543 rc = dispatch_peer_ni_cmd(prim_nid, nids[idx],
544 IOC_LIBCFS_DEL_PEER_NI, &data,
554 cYAML_build_error(rc, seq_no, DEL_CMD, "peer_ni", err_str, err_rc);
558 int lustre_lnet_config_route(char *nw, char *gw, int hops, int prio,
559 int seq_no, struct cYAML **err_rc)
561 struct lnet_ioctl_config_data data;
562 lnet_nid_t gateway_nid;
563 int rc = LUSTRE_CFG_RC_NO_ERR;
564 __u32 net = LNET_NIDNET(LNET_NID_ANY);
565 char err_str[LNET_MAX_STR_LEN];
567 snprintf(err_str, sizeof(err_str), "\"Success\"");
569 if (nw == NULL || gw == NULL) {
572 "\"missing mandatory parameter(s): '%s'\"",
573 (nw == NULL && gw == NULL) ? "network, gateway" :
574 (nw == NULL) ? "network" : "gateway");
575 rc = LUSTRE_CFG_RC_MISSING_PARAM;
579 net = libcfs_str2net(nw);
580 if (net == LNET_NIDNET(LNET_NID_ANY)) {
583 "\"cannot parse net %s\"", nw);
584 rc = LUSTRE_CFG_RC_BAD_PARAM;
588 gateway_nid = libcfs_str2nid(gw);
589 if (gateway_nid == LNET_NID_ANY) {
592 "\"cannot parse gateway NID '%s'\"", gw);
593 rc = LUSTRE_CFG_RC_BAD_PARAM;
598 /* hops is undefined */
599 hops = LNET_UNDEFINED_HOPS;
600 } else if (hops < 1 || hops > 255) {
603 "\"invalid hop count %d, must be between 1 and 255\"",
605 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
611 } else if (prio < 0) {
614 "\"invalid priority %d, must be greater than 0\"",
616 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
620 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
622 data.cfg_config_u.cfg_route.rtr_hop = hops;
623 data.cfg_config_u.cfg_route.rtr_priority = prio;
624 data.cfg_nid = gateway_nid;
626 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
631 "\"cannot add route: %s\"", strerror(errno));
636 cYAML_build_error(rc, seq_no, ADD_CMD, "route", err_str, err_rc);
641 int lustre_lnet_del_route(char *nw, char *gw,
642 int seq_no, struct cYAML **err_rc)
644 struct lnet_ioctl_config_data data;
645 lnet_nid_t gateway_nid;
646 int rc = LUSTRE_CFG_RC_NO_ERR;
647 __u32 net = LNET_NIDNET(LNET_NID_ANY);
648 char err_str[LNET_MAX_STR_LEN];
650 snprintf(err_str, sizeof(err_str), "\"Success\"");
652 if (nw == NULL || gw == NULL) {
655 "\"missing mandatory parameter(s): '%s'\"",
656 (nw == NULL && gw == NULL) ? "network, gateway" :
657 (nw == NULL) ? "network" : "gateway");
658 rc = LUSTRE_CFG_RC_MISSING_PARAM;
662 net = libcfs_str2net(nw);
663 if (net == LNET_NIDNET(LNET_NID_ANY)) {
666 "\"cannot parse net '%s'\"", nw);
667 rc = LUSTRE_CFG_RC_BAD_PARAM;
671 gateway_nid = libcfs_str2nid(gw);
672 if (gateway_nid == LNET_NID_ANY) {
675 "\"cannot parse gateway NID '%s'\"", gw);
676 rc = LUSTRE_CFG_RC_BAD_PARAM;
680 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
682 data.cfg_nid = gateway_nid;
684 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
689 "\"cannot delete route: %s\"", strerror(errno));
694 cYAML_build_error(rc, seq_no, DEL_CMD, "route", err_str, err_rc);
699 int lustre_lnet_show_route(char *nw, char *gw, int hops, int prio, int detail,
700 int seq_no, struct cYAML **show_rc,
701 struct cYAML **err_rc)
703 struct lnet_ioctl_config_data data;
704 lnet_nid_t gateway_nid;
705 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
707 __u32 net = LNET_NIDNET(LNET_NID_ANY);
709 struct cYAML *root = NULL, *route = NULL, *item = NULL;
710 struct cYAML *first_seq = NULL;
711 char err_str[LNET_MAX_STR_LEN];
714 snprintf(err_str, sizeof(err_str),
715 "\"out of memory\"");
718 net = libcfs_str2net(nw);
719 if (net == LNET_NIDNET(LNET_NID_ANY)) {
722 "\"cannot parse net '%s'\"", nw);
723 rc = LUSTRE_CFG_RC_BAD_PARAM;
728 /* show all routes without filtering on net */
729 net = LNET_NIDNET(LNET_NID_ANY);
733 gateway_nid = libcfs_str2nid(gw);
734 if (gateway_nid == LNET_NID_ANY) {
737 "\"cannot parse gateway NID '%s'\"", gw);
738 rc = LUSTRE_CFG_RC_BAD_PARAM;
742 /* show all routes with out filtering on gateway */
743 gateway_nid = LNET_NID_ANY;
745 if ((hops < 1 && hops != -1) || hops > 255) {
748 "\"invalid hop count %d, must be between 0 and 256\"",
750 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
754 /* create struct cYAML root object */
755 root = cYAML_create_object(NULL, NULL);
759 route = cYAML_create_seq(root, "route");
764 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
767 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
773 /* filter on provided data */
774 if (net != LNET_NIDNET(LNET_NID_ANY) &&
778 if (gateway_nid != LNET_NID_ANY &&
779 gateway_nid != data.cfg_nid)
783 hops != data.cfg_config_u.cfg_route.rtr_hop)
787 prio != data.cfg_config_u.cfg_route.rtr_priority)
790 /* default rc to -1 incase we hit the goto */
794 item = cYAML_create_seq_item(route);
798 if (first_seq == NULL)
801 if (cYAML_create_string(item, "net",
802 libcfs_net2str(data.cfg_net)) == NULL)
805 if (cYAML_create_string(item, "gateway",
806 libcfs_nid2str(data.cfg_nid)) == NULL)
810 if (cYAML_create_number(item, "hop",
811 (int) data.cfg_config_u.
812 cfg_route.rtr_hop) ==
816 if (cYAML_create_number(item, "priority",
818 cfg_route.rtr_priority) == NULL)
821 if (cYAML_create_string(item, "state",
822 data.cfg_config_u.cfg_route.
824 "up" : "down") == NULL)
829 /* print output iff show_rc is not provided */
831 cYAML_print_tree(root);
833 if (l_errno != ENOENT) {
836 "\"cannot get routes: %s\"",
841 rc = LUSTRE_CFG_RC_NO_ERR;
843 snprintf(err_str, sizeof(err_str), "\"success\"");
845 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
846 cYAML_free_tree(root);
847 } else if (show_rc != NULL && *show_rc != NULL) {
848 struct cYAML *show_node;
849 /* find the route node, if one doesn't exist then
850 * insert one. Otherwise add to the one there
852 show_node = cYAML_get_object_item(*show_rc, "route");
853 if (show_node != NULL && cYAML_is_sequence(show_node)) {
854 cYAML_insert_child(show_node, first_seq);
857 } else if (show_node == NULL) {
858 cYAML_insert_sibling((*show_rc)->cy_child,
862 cYAML_free_tree(root);
868 cYAML_build_error(rc, seq_no, SHOW_CMD, "route", err_str, err_rc);
873 static int socket_intf_query(int request, char *intf,
879 if (strlen(intf) >= IFNAMSIZ || ifr == NULL)
880 return LUSTRE_CFG_RC_BAD_PARAM;
882 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
884 return LUSTRE_CFG_RC_BAD_PARAM;
886 strcpy(ifr->ifr_name, intf);
887 rc = ioctl(sockfd, request, ifr);
889 rc = LUSTRE_CFG_RC_BAD_PARAM;
897 * for each interface in the array of interfaces find the IP address of
898 * that interface, create its nid and add it to an array of NIDs.
899 * Stop if any of the interfaces is down
901 static int lustre_lnet_intf2nids(struct lnet_dlc_network_descr *nw,
902 lnet_nid_t **nids, __u32 *nnids)
904 int i = 0, count = 0, rc;
907 struct lnet_dlc_intf_descr *intf;
909 if (nw == NULL || nids == NULL)
910 return LUSTRE_CFG_RC_BAD_PARAM;
912 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network)
915 *nids = calloc(count, sizeof(lnet_nid_t));
917 return LUSTRE_CFG_RC_OUT_OF_MEM;
919 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network) {
920 memset(&ifr, 0, sizeof(ifr));
921 rc = socket_intf_query(SIOCGIFFLAGS, intf->intf_name, &ifr);
925 if ((ifr.ifr_flags & IFF_UP) == 0) {
926 rc = LUSTRE_CFG_RC_BAD_PARAM;
930 memset(&ifr, 0, sizeof(ifr));
931 rc = socket_intf_query(SIOCGIFADDR, intf->intf_name, &ifr);
935 ip = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
937 (*nids)[i] = LNET_MKNID(nw->nw_id, ip);
952 * called repeatedly until a match or no more ip range
954 * ip_range expression
955 * interface list with all the interface names.
956 * all the interfaces in the system.
958 * try to match the ip_range expr to one of the interfaces' IPs in
959 * the system. If we hit a patch for an interface. Check if that
960 * interface name is in the list.
962 * If there are more than one interface in the list, then make sure
963 * that the IPs for all of these interfaces match the ip ranges
966 * for each interface in intf_list
967 * look up the intf name in ifa
968 * if not there then no match
969 * check ip obtained from ifa against a match to any of the
971 * If no match, then fail
973 * The result is that all the interfaces have to match.
975 int lustre_lnet_match_ip_to_intf(struct ifaddrs *ifa,
976 struct list_head *intf_list,
977 struct list_head *ip_ranges)
981 struct lnet_dlc_intf_descr *intf_descr, *tmp;
982 struct ifaddrs *ifaddr = ifa;
983 struct lustre_lnet_ip_range_descr *ip_range;
987 * if there are no explicit interfaces, and no ip ranges, then
988 * configure the first tcp interface we encounter.
990 if (list_empty(intf_list) && list_empty(ip_ranges)) {
991 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
992 if (ifaddr->ifa_addr == NULL)
995 if ((ifaddr->ifa_flags & IFF_UP) == 0)
998 family = ifaddr->ifa_addr->sa_family;
999 if (family == AF_INET &&
1000 strcmp(ifaddr->ifa_name, "lo") != 0) {
1001 rc = lustre_lnet_add_intf_descr
1002 (intf_list, ifaddr->ifa_name,
1003 strlen(ifaddr->ifa_name));
1005 if (rc != LUSTRE_CFG_RC_NO_ERR)
1008 return LUSTRE_CFG_RC_MATCH;
1011 return LUSTRE_CFG_RC_NO_MATCH;
1015 * First interface which matches an IP pattern will be used
1017 if (list_empty(intf_list)) {
1019 * no interfaces provided in the rule, but an ip range is
1020 * provided, so try and match an interface to the ip
1023 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1024 if (ifaddr->ifa_addr == NULL)
1027 if ((ifaddr->ifa_flags & IFF_UP) == 0)
1030 family = ifaddr->ifa_addr->sa_family;
1031 if (family == AF_INET) {
1032 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->
1035 list_for_each_entry(ip_range, ip_ranges,
1037 rc = cfs_ip_addr_match(bswap_32(ip),
1038 &ip_range->ipr_expr);
1042 rc = lustre_lnet_add_intf_descr
1043 (intf_list, ifaddr->ifa_name,
1044 strlen(ifaddr->ifa_name));
1046 if (rc != LUSTRE_CFG_RC_NO_ERR)
1052 if (!list_empty(intf_list))
1053 return LUSTRE_CFG_RC_MATCH;
1055 return LUSTRE_CFG_RC_NO_MATCH;
1059 * If an interface is explicitly specified the ip-range might or
1060 * might not be specified. if specified the interface needs to match the
1061 * ip-range. If no ip-range then the interfaces are
1062 * automatically matched if they are all up.
1063 * If > 1 interfaces all the interfaces must match for the NI to
1066 list_for_each_entry_safe(intf_descr, tmp, intf_list, intf_on_network) {
1067 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1068 if (ifaddr->ifa_addr == NULL)
1071 family = ifaddr->ifa_addr->sa_family;
1072 if (family == AF_INET &&
1073 strcmp(intf_descr->intf_name,
1074 ifaddr->ifa_name) == 0)
1078 if (ifaddr == NULL) {
1079 list_del(&intf_descr->intf_on_network);
1080 free_intf_descr(intf_descr);
1084 if ((ifaddr->ifa_flags & IFF_UP) == 0) {
1085 list_del(&intf_descr->intf_on_network);
1086 free_intf_descr(intf_descr);
1090 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr.s_addr;
1093 list_for_each_entry(ip_range, ip_ranges, ipr_entry) {
1094 rc = cfs_ip_addr_match(bswap_32(ip), &ip_range->ipr_expr);
1100 /* no match for this interface */
1101 list_del(&intf_descr->intf_on_network);
1102 free_intf_descr(intf_descr);
1106 return LUSTRE_CFG_RC_MATCH;
1109 int lustre_lnet_resolve_ip2nets_rule(struct lustre_lnet_ip2nets *ip2nets,
1110 lnet_nid_t **nids, __u32 *nnids)
1112 struct ifaddrs *ifa;
1113 int rc = LUSTRE_CFG_RC_NO_ERR;
1115 rc = getifaddrs(&ifa);
1119 rc = lustre_lnet_match_ip_to_intf(ifa,
1120 &ip2nets->ip2nets_net.nw_intflist,
1121 &ip2nets->ip2nets_ip_ranges);
1122 if (rc != LUSTRE_CFG_RC_MATCH) {
1127 rc = lustre_lnet_intf2nids(&ip2nets->ip2nets_net, nids, nnids);
1128 if (rc != LUSTRE_CFG_RC_NO_ERR) {
1139 lustre_lnet_ioctl_config_ni(struct list_head *intf_list,
1140 struct lnet_ioctl_config_lnd_tunables *tunables,
1141 struct cfs_expr_list *global_cpts,
1142 lnet_nid_t *nids, char *err_str)
1145 struct lnet_ioctl_config_ni *conf;
1146 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1147 int rc = LUSTRE_CFG_RC_NO_ERR, i = 0;
1150 struct lnet_dlc_intf_descr *intf_descr;
1152 struct cfs_expr_list *cpt_expr;
1154 list_for_each_entry(intf_descr, intf_list,
1156 if (i == 0 && tunables != NULL)
1157 len = sizeof(struct lnet_ioctl_config_ni) +
1158 sizeof(struct lnet_ioctl_config_lnd_tunables);
1160 len = sizeof(struct lnet_ioctl_config_ni);
1162 data = calloc(1, len);
1164 return LUSTRE_CFG_RC_OUT_OF_MEM;
1165 conf = (struct lnet_ioctl_config_ni*) data;
1166 if (i == 0 && tunables != NULL)
1167 tun = (struct lnet_ioctl_config_lnd_tunables*)
1170 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1171 conf->lic_cfg_hdr.ioc_len = len;
1172 conf->lic_nid = nids[i];
1173 strncpy(conf->lic_ni_intf[0], intf_descr->intf_name,
1176 if (intf_descr->cpt_expr != NULL)
1177 cpt_expr = intf_descr->cpt_expr;
1178 else if (global_cpts != NULL)
1179 cpt_expr = global_cpts;
1183 if (cpt_expr != NULL) {
1184 count = cfs_expr_list_values(cpt_expr,
1185 LNET_MAX_SHOW_NUM_CPT,
1188 memcpy(conf->lic_cpts, cpt_array,
1189 sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1198 conf->lic_ncpts = count;
1200 if (i == 0 && tunables != NULL)
1201 /* TODO put in the LND tunables */
1202 memcpy(tun, tunables, sizeof(*tunables));
1204 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1209 "\"cannot add network: %s\"", strerror(errno));
1217 return LUSTRE_CFG_RC_NO_ERR;
1221 lustre_lnet_config_ip2nets(struct lustre_lnet_ip2nets *ip2nets,
1222 struct lnet_ioctl_config_lnd_tunables *tunables,
1223 struct cfs_expr_list *global_cpts,
1224 int seq_no, struct cYAML **err_rc)
1226 lnet_nid_t *nids = NULL;
1229 char err_str[LNET_MAX_STR_LEN];
1231 snprintf(err_str, sizeof(err_str), "\"success\"");
1236 "\"incomplete ip2nets information\"");
1237 rc = LUSTRE_CFG_RC_BAD_PARAM;
1242 * call below function to resolve the rules into a list of nids.
1243 * The memory is allocated in that function then freed here when
1244 * it's no longer needed.
1246 rc = lustre_lnet_resolve_ip2nets_rule(ip2nets, &nids, &nnids);
1247 if (rc != LUSTRE_CFG_RC_NO_ERR && rc != LUSTRE_CFG_RC_MATCH) {
1250 "\"cannot resolve ip2nets rule\"");
1254 if (list_empty(&ip2nets->ip2nets_net.nw_intflist)) {
1255 snprintf(err_str, sizeof(err_str),
1256 "\"no interfaces match ip2nets rules\"");
1260 rc = lustre_lnet_ioctl_config_ni(&ip2nets->ip2nets_net.nw_intflist,
1261 tunables, global_cpts, nids,
1268 cYAML_build_error(rc, seq_no, ADD_CMD, "ip2nets", err_str, err_rc);
1272 int lustre_lnet_config_ni(struct lnet_dlc_network_descr *nw_descr,
1273 struct cfs_expr_list *global_cpts,
1275 struct lnet_ioctl_config_lnd_tunables *tunables,
1276 int seq_no, struct cYAML **err_rc)
1279 struct lnet_ioctl_config_ni *conf;
1280 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1281 char buf[LNET_MAX_STR_LEN];
1282 int rc = LUSTRE_CFG_RC_NO_ERR;
1283 char err_str[LNET_MAX_STR_LEN];
1284 lnet_nid_t *nids = NULL;
1288 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1291 snprintf(err_str, sizeof(err_str), "\"success\"");
1293 if (ip2net == NULL && nw_descr == NULL) {
1296 "\"mandatory parameters not specified.\"");
1297 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1301 if (ip2net != NULL && strlen(ip2net) >= sizeof(buf)) {
1304 "\"ip2net string too long %d\"",
1305 (int)strlen(ip2net));
1306 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1310 if (ip2net != NULL) {
1311 if (tunables != NULL)
1312 len = sizeof(struct lnet_ioctl_config_ni) +
1313 sizeof(struct lnet_ioctl_config_lnd_tunables);
1315 len = sizeof(struct lnet_ioctl_config_ni);
1316 data = calloc(1, len);
1318 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1321 conf = (struct lnet_ioctl_config_ni*) data;
1322 if (tunables != NULL)
1323 tun = (struct lnet_ioctl_config_lnd_tunables*)
1324 (data + sizeof(*conf));
1326 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1327 conf->lic_cfg_hdr.ioc_len = len;
1328 strncpy(conf->lic_legacy_ip2nets, ip2net,
1331 if (global_cpts != NULL) {
1332 count = cfs_expr_list_values(global_cpts,
1333 LNET_MAX_SHOW_NUM_CPT,
1336 memcpy(conf->lic_cpts, cpt_array,
1337 sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1346 conf->lic_ncpts = count;
1348 if (tunables != NULL)
1349 memcpy(tun, tunables, sizeof(*tunables));
1351 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1356 "\"cannot add network: %s\"", strerror(errno));
1363 if (LNET_NETTYP(nw_descr->nw_id) == LOLND) {
1364 rc = LUSTRE_CFG_RC_NO_ERR;
1368 if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1371 "\"cannot parse net '%s'\"",
1372 libcfs_net2str(nw_descr->nw_id));
1373 rc = LUSTRE_CFG_RC_BAD_PARAM;
1377 if (list_empty(&nw_descr->nw_intflist)) {
1380 "\"no interface name provided\"");
1381 rc = LUSTRE_CFG_RC_BAD_PARAM;
1385 rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids);
1387 snprintf(err_str, sizeof(err_str),
1388 "\"bad parameter\"");
1389 rc = LUSTRE_CFG_RC_BAD_PARAM;
1393 rc = lustre_lnet_ioctl_config_ni(&nw_descr->nw_intflist,
1394 tunables, global_cpts, nids,
1398 if (nw_descr != NULL) {
1399 list_for_each_entry_safe(intf_descr, tmp,
1400 &nw_descr->nw_intflist,
1402 list_del(&intf_descr->intf_on_network);
1403 free_intf_descr(intf_descr);
1407 cYAML_build_error(rc, seq_no, ADD_CMD, "net", err_str, err_rc);
1418 int lustre_lnet_del_ni(struct lnet_dlc_network_descr *nw_descr,
1419 int seq_no, struct cYAML **err_rc)
1421 struct lnet_ioctl_config_ni data;
1422 int rc = LUSTRE_CFG_RC_NO_ERR, i;
1423 char err_str[LNET_MAX_STR_LEN];
1424 lnet_nid_t *nids = NULL;
1426 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1428 snprintf(err_str, sizeof(err_str), "\"success\"");
1430 if (nw_descr == NULL) {
1433 "\"missing mandatory parameter\"");
1434 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1438 if (LNET_NETTYP(nw_descr->nw_id) == LOLND)
1439 return LUSTRE_CFG_RC_NO_ERR;
1441 if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1444 "\"cannot parse net '%s'\"",
1445 libcfs_net2str(nw_descr->nw_id));
1446 rc = LUSTRE_CFG_RC_BAD_PARAM;
1450 rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids);
1452 snprintf(err_str, sizeof(err_str),
1453 "\"bad parameter\"");
1454 rc = LUSTRE_CFG_RC_BAD_PARAM;
1459 * no interfaces just the nw_id is specified
1462 nids = calloc(1, sizeof(*nids));
1464 snprintf(err_str, sizeof(err_str),
1465 "\"out of memory\"");
1466 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1469 nids[0] = LNET_MKNID(nw_descr->nw_id, 0);
1473 for (i = 0; i < nnids; i++) {
1474 LIBCFS_IOC_INIT_V2(data, lic_cfg_hdr);
1475 data.lic_nid = nids[i];
1477 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_LOCAL_NI, &data);
1482 "\"cannot del network: %s\"", strerror(errno));
1486 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
1488 list_del(&intf_descr->intf_on_network);
1489 free_intf_descr(intf_descr);
1493 cYAML_build_error(rc, seq_no, DEL_CMD, "net", err_str, err_rc);
1501 int lustre_lnet_show_net(char *nw, int detail, int seq_no,
1502 struct cYAML **show_rc, struct cYAML **err_rc)
1505 struct lnet_ioctl_config_ni *ni_data;
1506 struct lnet_ioctl_config_lnd_tunables *lnd;
1507 struct lnet_ioctl_element_stats *stats;
1508 __u32 net = LNET_NIDNET(LNET_NID_ANY);
1509 __u32 prev_net = LNET_NIDNET(LNET_NID_ANY);
1510 int rc = LUSTRE_CFG_RC_OUT_OF_MEM, i, j;
1512 struct cYAML *root = NULL, *tunables = NULL,
1513 *net_node = NULL, *interfaces = NULL,
1514 *item = NULL, *first_seq = NULL,
1515 *tmp = NULL, *statistics = NULL;
1516 int str_buf_len = LNET_MAX_SHOW_NUM_CPT * 2;
1517 char str_buf[str_buf_len];
1519 char err_str[LNET_MAX_STR_LEN];
1520 bool exist = false, new_net = true;
1522 size_t buf_size = sizeof(*ni_data) + sizeof(*lnd) + sizeof(*stats);
1524 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1526 buf = calloc(1, buf_size);
1530 ni_data = (struct lnet_ioctl_config_ni *)buf;
1533 net = libcfs_str2net(nw);
1534 if (net == LNET_NIDNET(LNET_NID_ANY)) {
1537 "\"cannot parse net '%s'\"", nw);
1538 rc = LUSTRE_CFG_RC_BAD_PARAM;
1543 root = cYAML_create_object(NULL, NULL);
1547 net_node = cYAML_create_seq(root, "net");
1548 if (net_node == NULL)
1555 memset(buf, 0, buf_size);
1557 LIBCFS_IOC_INIT_V2(*ni_data, lic_cfg_hdr);
1559 * set the ioc_len to the proper value since INIT assumes
1562 ni_data->lic_cfg_hdr.ioc_len = buf_size;
1563 ni_data->lic_idx = i;
1565 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LOCAL_NI, ni_data);
1571 rc_net = LNET_NIDNET(ni_data->lic_nid);
1573 /* filter on provided data */
1574 if (net != LNET_NIDNET(LNET_NID_ANY) &&
1578 /* default rc to -1 in case we hit the goto */
1582 stats = (struct lnet_ioctl_element_stats *)ni_data->lic_bulk;
1583 lnd = (struct lnet_ioctl_config_lnd_tunables *)
1584 (ni_data->lic_bulk + sizeof(*stats));
1586 if (rc_net != prev_net) {
1593 if (!cYAML_create_string(net_node, "net type",
1594 libcfs_net2str(rc_net)))
1597 tmp = cYAML_create_seq(net_node, "local NI(s)");
1603 /* create the tree to be printed. */
1604 item = cYAML_create_seq_item(tmp);
1608 if (first_seq == NULL)
1611 if (cYAML_create_string(item, "nid",
1612 libcfs_nid2str(ni_data->lic_nid)) == NULL)
1615 if (cYAML_create_string(item,
1617 (ni_data->lic_status ==
1618 LNET_NI_STATUS_UP) ?
1619 "up" : "down") == NULL)
1622 /* don't add interfaces unless there is at least one
1624 if (strlen(ni_data->lic_ni_intf[0]) > 0) {
1625 interfaces = cYAML_create_object(item, "interfaces");
1626 if (interfaces == NULL)
1629 for (j = 0; j < LNET_NUM_INTERFACES; j++) {
1630 if (strlen(ni_data->lic_ni_intf[j]) > 0) {
1632 sizeof(str_buf), "%d", j);
1633 if (cYAML_create_string(interfaces,
1635 ni_data->lic_ni_intf[j]) ==
1645 statistics = cYAML_create_object(item, "statistics");
1646 if (statistics == NULL)
1649 if (cYAML_create_number(statistics, "send_count",
1650 stats->iel_send_count)
1654 if (cYAML_create_number(statistics, "recv_count",
1655 stats->iel_recv_count)
1659 if (cYAML_create_number(statistics, "drop_count",
1660 stats->iel_drop_count)
1664 tunables = cYAML_create_object(item, "tunables");
1668 rc = lustre_net_show_tunables(tunables, &lnd->lt_cmn);
1669 if (rc != LUSTRE_CFG_RC_NO_ERR)
1672 tunables = cYAML_create_object(item, "lnd tunables");
1673 if (tunables == NULL)
1676 rc = lustre_ni_show_tunables(tunables, LNET_NETTYP(rc_net),
1678 if (rc != LUSTRE_CFG_RC_NO_ERR)
1681 if (cYAML_create_number(item, "tcp bonding",
1682 ni_data->lic_tcp_bonding)
1686 if (cYAML_create_number(item, "dev cpt",
1687 ni_data->lic_dev_cpt) == NULL)
1690 /* out put the CPTs in the format: "[x,x,x,...]" */
1691 limit = str_buf + str_buf_len - 3;
1692 pos += snprintf(pos, limit - pos, "\"[");
1693 for (j = 0 ; ni_data->lic_ncpts >= 1 &&
1694 j < ni_data->lic_ncpts &&
1696 pos += snprintf(pos, limit - pos,
1697 "%d", ni_data->lic_cpts[j]);
1698 if ((j + 1) < ni_data->lic_ncpts)
1699 pos += snprintf(pos, limit - pos, ",");
1701 pos += snprintf(pos, 3, "]\"");
1703 if (ni_data->lic_ncpts >= 1 &&
1704 cYAML_create_string(item, "CPT",
1710 /* Print out the net information only if show_rc is not provided */
1711 if (show_rc == NULL)
1712 cYAML_print_tree(root);
1714 if (l_errno != ENOENT) {
1717 "\"cannot get networks: %s\"",
1722 rc = LUSTRE_CFG_RC_NO_ERR;
1724 snprintf(err_str, sizeof(err_str), "\"success\"");
1726 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
1727 cYAML_free_tree(root);
1728 } else if (show_rc != NULL && *show_rc != NULL) {
1729 struct cYAML *show_node;
1730 /* find the net node, if one doesn't exist
1731 * then insert one. Otherwise add to the one there
1733 show_node = cYAML_get_object_item(*show_rc, "net");
1734 if (show_node != NULL && cYAML_is_sequence(show_node)) {
1735 cYAML_insert_child(show_node, first_seq);
1738 } else if (show_node == NULL) {
1739 cYAML_insert_sibling((*show_rc)->cy_child,
1743 cYAML_free_tree(root);
1749 cYAML_build_error(rc, seq_no, SHOW_CMD, "net", err_str, err_rc);
1754 int lustre_lnet_enable_routing(int enable, int seq_no, struct cYAML **err_rc)
1756 struct lnet_ioctl_config_data data;
1757 int rc = LUSTRE_CFG_RC_NO_ERR;
1758 char err_str[LNET_MAX_STR_LEN];
1760 snprintf(err_str, sizeof(err_str), "\"success\"");
1762 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1763 data.cfg_config_u.cfg_buffers.buf_enable = (enable) ? 1 : 0;
1765 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIG_RTR, &data);
1770 "\"cannot %s routing %s\"",
1771 (enable) ? "enable" : "disable", strerror(errno));
1776 cYAML_build_error(rc, seq_no,
1777 (enable) ? ADD_CMD : DEL_CMD,
1778 "routing", err_str, err_rc);
1783 int lustre_lnet_config_numa_range(int range, int seq_no, struct cYAML **err_rc)
1785 struct lnet_ioctl_numa_range data;
1786 int rc = LUSTRE_CFG_RC_NO_ERR;
1787 char err_str[LNET_MAX_STR_LEN];
1789 snprintf(err_str, sizeof(err_str), "\"success\"");
1794 "\"range must be >= 0\"");
1795 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1799 LIBCFS_IOC_INIT_V2(data, nr_hdr);
1800 data.nr_range = range;
1802 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_SET_NUMA_RANGE, &data);
1807 "\"cannot configure buffers: %s\"", strerror(errno));
1812 cYAML_build_error(rc, seq_no, ADD_CMD, "numa_range", err_str, err_rc);
1817 int lustre_lnet_config_buffers(int tiny, int small, int large, int seq_no,
1818 struct cYAML **err_rc)
1820 struct lnet_ioctl_config_data data;
1821 int rc = LUSTRE_CFG_RC_NO_ERR;
1822 char err_str[LNET_MAX_STR_LEN];
1824 snprintf(err_str, sizeof(err_str), "\"success\"");
1826 /* -1 indicates to ignore changes to this field */
1827 if (tiny < -1 || small < -1 || large < -1) {
1830 "\"tiny, small and large must be >= 0\"");
1831 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1835 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1836 data.cfg_config_u.cfg_buffers.buf_tiny = tiny;
1837 data.cfg_config_u.cfg_buffers.buf_small = small;
1838 data.cfg_config_u.cfg_buffers.buf_large = large;
1840 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_BUF, &data);
1845 "\"cannot configure buffers: %s\"", strerror(errno));
1850 cYAML_build_error(rc, seq_no, ADD_CMD, "buf", err_str, err_rc);
1855 int lustre_lnet_show_routing(int seq_no, struct cYAML **show_rc,
1856 struct cYAML **err_rc)
1858 struct lnet_ioctl_config_data *data;
1859 struct lnet_ioctl_pool_cfg *pool_cfg = NULL;
1860 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1863 char *pools[LNET_NRBPOOLS] = {"tiny", "small", "large"};
1864 int buf_count[LNET_NRBPOOLS] = {0};
1865 struct cYAML *root = NULL, *pools_node = NULL,
1866 *type_node = NULL, *item = NULL, *cpt = NULL,
1867 *first_seq = NULL, *buffers = NULL;
1869 char err_str[LNET_MAX_STR_LEN];
1870 char node_name[LNET_MAX_STR_LEN];
1873 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1875 buf = calloc(1, sizeof(*data) + sizeof(*pool_cfg));
1879 data = (struct lnet_ioctl_config_data *)buf;
1881 root = cYAML_create_object(NULL, NULL);
1885 pools_node = cYAML_create_seq(root, "routing");
1886 if (pools_node == NULL)
1890 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
1891 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
1892 sizeof(struct lnet_ioctl_pool_cfg);
1893 data->cfg_count = i;
1895 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_BUF, data);
1903 pool_cfg = (struct lnet_ioctl_pool_cfg *)data->cfg_bulk;
1905 snprintf(node_name, sizeof(node_name), "cpt[%d]", i);
1906 item = cYAML_create_seq_item(pools_node);
1910 if (first_seq == NULL)
1913 cpt = cYAML_create_object(item, node_name);
1917 /* create the tree and print */
1918 for (j = 0; j < LNET_NRBPOOLS; j++) {
1919 type_node = cYAML_create_object(cpt, pools[j]);
1920 if (type_node == NULL)
1922 if (cYAML_create_number(type_node, "npages",
1923 pool_cfg->pl_pools[j].pl_npages)
1926 if (cYAML_create_number(type_node, "nbuffers",
1927 pool_cfg->pl_pools[j].
1928 pl_nbuffers) == NULL)
1930 if (cYAML_create_number(type_node, "credits",
1931 pool_cfg->pl_pools[j].
1932 pl_credits) == NULL)
1934 if (cYAML_create_number(type_node, "mincredits",
1935 pool_cfg->pl_pools[j].
1936 pl_mincredits) == NULL)
1938 /* keep track of the total count for each of the
1939 * tiny, small and large buffers */
1940 buf_count[j] += pool_cfg->pl_pools[j].pl_nbuffers;
1944 if (pool_cfg != NULL) {
1945 item = cYAML_create_seq_item(pools_node);
1949 if (cYAML_create_number(item, "enable", pool_cfg->pl_routing) ==
1954 /* create a buffers entry in the show. This is necessary so that
1955 * if the YAML output is used to configure a node, the buffer
1956 * configuration takes hold */
1957 buffers = cYAML_create_object(root, "buffers");
1958 if (buffers == NULL)
1961 for (i = 0; i < LNET_NRBPOOLS; i++) {
1962 if (cYAML_create_number(buffers, pools[i], buf_count[i]) == NULL)
1966 if (show_rc == NULL)
1967 cYAML_print_tree(root);
1969 if (l_errno != ENOENT) {
1972 "\"cannot get routing information: %s\"",
1977 rc = LUSTRE_CFG_RC_NO_ERR;
1979 snprintf(err_str, sizeof(err_str), "\"success\"");
1980 rc = LUSTRE_CFG_RC_NO_ERR;
1984 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
1985 cYAML_free_tree(root);
1986 } else if (show_rc != NULL && *show_rc != NULL) {
1987 struct cYAML *routing_node;
1988 /* there should exist only one routing block and one
1989 * buffers block. If there already exists a previous one
1990 * then don't add another */
1991 routing_node = cYAML_get_object_item(*show_rc, "routing");
1992 if (routing_node == NULL) {
1993 cYAML_insert_sibling((*show_rc)->cy_child,
1997 cYAML_free_tree(root);
2003 cYAML_build_error(rc, seq_no, SHOW_CMD, "routing", err_str, err_rc);
2008 int lustre_lnet_show_peer(char *knid, int detail, int seq_no,
2009 struct cYAML **show_rc, struct cYAML **err_rc)
2012 * TODO: This function is changing in a future patch to accommodate
2013 * PEER_LIST and proper filtering on any nid of the peer
2015 struct lnet_ioctl_peer_cfg peer_info;
2016 struct lnet_peer_ni_credit_info *lpni_cri;
2017 struct lnet_ioctl_element_stats *lpni_stats;
2018 int rc = LUSTRE_CFG_RC_OUT_OF_MEM, ncpt = 0, i = 0, j = 0;
2020 struct cYAML *root = NULL, *peer = NULL, *peer_ni = NULL,
2021 *first_seq = NULL, *peer_root = NULL, *tmp = NULL;
2022 char err_str[LNET_MAX_STR_LEN];
2023 lnet_nid_t prev_primary_nid = LNET_NID_ANY, primary_nid = LNET_NID_ANY;
2024 int data_size = sizeof(*lpni_cri) + sizeof(*lpni_stats);
2025 char *data = malloc(data_size);
2026 bool new_peer = true;
2028 snprintf(err_str, sizeof(err_str),
2029 "\"out of memory\"");
2034 /* create struct cYAML root object */
2035 root = cYAML_create_object(NULL, NULL);
2039 peer_root = cYAML_create_seq(root, "peer");
2040 if (peer_root == NULL)
2044 primary_nid = libcfs_str2nid(knid);
2048 memset(data, 0, data_size);
2049 memset(&peer_info, 0, sizeof(peer_info));
2050 LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2051 peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2052 peer_info.prcfg_count = i;
2053 peer_info.prcfg_bulk = (void *)data;
2054 peer_info.prcfg_size = data_size;
2056 rc = l_ioctl(LNET_DEV_ID,
2057 IOC_LIBCFS_GET_PEER_NI, &peer_info);
2063 if (primary_nid != LNET_NID_ANY &&
2064 primary_nid != peer_info.prcfg_prim_nid)
2067 lpni_cri = peer_info.prcfg_bulk;
2068 lpni_stats = peer_info.prcfg_bulk + sizeof(*lpni_cri);
2070 peer = cYAML_create_seq_item(peer_root);
2074 if (peer_info.prcfg_prim_nid != prev_primary_nid) {
2075 prev_primary_nid = peer_info.prcfg_prim_nid;
2080 lnet_nid_t pnid = peer_info.prcfg_prim_nid;
2081 if (cYAML_create_string(peer, "primary nid",
2082 libcfs_nid2str(pnid))
2085 if (cYAML_create_string(peer, "Multi-Rail",
2086 peer_info.prcfg_mr ?
2090 tmp = cYAML_create_seq(peer, "peer ni");
2096 if (first_seq == NULL)
2099 peer_ni = cYAML_create_seq_item(tmp);
2100 if (peer_ni == NULL)
2103 if (cYAML_create_string(peer_ni, "nid",
2105 (peer_info.prcfg_cfg_nid))
2109 if (cYAML_create_string(peer_ni, "state",
2110 lpni_cri->cr_aliveness)
2117 if (cYAML_create_number(peer_ni, "max_ni_tx_credits",
2118 lpni_cri->cr_ni_peer_tx_credits)
2122 if (cYAML_create_number(peer_ni, "available_tx_credits",
2123 lpni_cri->cr_peer_tx_credits)
2127 if (cYAML_create_number(peer_ni, "min_tx_credits",
2128 lpni_cri->cr_peer_min_tx_credits)
2132 if (cYAML_create_number(peer_ni, "tx_q_num_of_buf",
2133 lpni_cri->cr_peer_tx_qnob)
2137 if (cYAML_create_number(peer_ni, "available_rtr_credits",
2138 lpni_cri->cr_peer_rtr_credits)
2142 if (cYAML_create_number(peer_ni, "min_rtr_credits",
2143 lpni_cri->cr_peer_min_rtr_credits)
2147 if (cYAML_create_number(peer_ni, "send_count",
2148 lpni_stats->iel_send_count)
2152 if (cYAML_create_number(peer_ni, "recv_count",
2153 lpni_stats->iel_recv_count)
2157 if (cYAML_create_number(peer_ni, "drop_count",
2158 lpni_stats->iel_drop_count)
2162 if (cYAML_create_number(peer_ni, "refcount",
2163 lpni_cri->cr_refcount) == NULL)
2167 if (l_errno != ENOENT) {
2170 "\"cannot get peer information: %s\"",
2179 /* print output iff show_rc is not provided */
2180 if (show_rc == NULL)
2181 cYAML_print_tree(root);
2183 snprintf(err_str, sizeof(err_str), "\"success\"");
2184 rc = LUSTRE_CFG_RC_NO_ERR;
2187 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2188 cYAML_free_tree(root);
2189 } else if (show_rc != NULL && *show_rc != NULL) {
2190 struct cYAML *show_node;
2191 /* find the peer node, if one doesn't exist then
2192 * insert one. Otherwise add to the one there
2194 show_node = cYAML_get_object_item(*show_rc,
2196 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2197 cYAML_insert_child(show_node, first_seq);
2200 } else if (show_node == NULL) {
2201 cYAML_insert_sibling((*show_rc)->cy_child,
2205 cYAML_free_tree(root);
2211 cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
2217 int lustre_lnet_show_numa_range(int seq_no, struct cYAML **show_rc,
2218 struct cYAML **err_rc)
2220 struct lnet_ioctl_numa_range data;
2223 char err_str[LNET_MAX_STR_LEN];
2224 struct cYAML *root = NULL, *range = NULL;
2226 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2228 LIBCFS_IOC_INIT_V2(data, nr_hdr);
2230 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NUMA_RANGE, &data);
2235 "\"cannot get numa range: %s\"",
2241 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2243 root = cYAML_create_object(NULL, NULL);
2247 range = cYAML_create_object(root, "numa");
2251 if (cYAML_create_number(range, "range",
2252 data.nr_range) == NULL)
2255 if (show_rc == NULL)
2256 cYAML_print_tree(root);
2258 snprintf(err_str, sizeof(err_str), "\"success\"");
2259 rc = LUSTRE_CFG_RC_NO_ERR;
2261 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2262 cYAML_free_tree(root);
2263 } else if (show_rc != NULL && *show_rc != NULL) {
2264 cYAML_insert_sibling((*show_rc)->cy_child,
2271 cYAML_build_error(rc, seq_no, SHOW_CMD, "numa", err_str, err_rc);
2276 int lustre_lnet_show_stats(int seq_no, struct cYAML **show_rc,
2277 struct cYAML **err_rc)
2279 struct lnet_ioctl_lnet_stats data;
2282 char err_str[LNET_MAX_STR_LEN];
2283 struct cYAML *root = NULL, *stats = NULL;
2285 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2287 LIBCFS_IOC_INIT_V2(data, st_hdr);
2289 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LNET_STATS, &data);
2294 "\"cannot get lnet statistics: %s\"",
2300 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2302 root = cYAML_create_object(NULL, NULL);
2306 stats = cYAML_create_object(root, "statistics");
2310 if (cYAML_create_number(stats, "msgs_alloc",
2311 data.st_cntrs.msgs_alloc) == NULL)
2314 if (cYAML_create_number(stats, "msgs_max",
2315 data.st_cntrs.msgs_max) == NULL)
2318 if (cYAML_create_number(stats, "errors",
2319 data.st_cntrs.errors) == NULL)
2322 if (cYAML_create_number(stats, "send_count",
2323 data.st_cntrs.send_count) == NULL)
2326 if (cYAML_create_number(stats, "recv_count",
2327 data.st_cntrs.recv_count) == NULL)
2330 if (cYAML_create_number(stats, "route_count",
2331 data.st_cntrs.route_count) == NULL)
2334 if (cYAML_create_number(stats, "drop_count",
2335 data.st_cntrs.drop_count) == NULL)
2338 if (cYAML_create_number(stats, "send_length",
2339 data.st_cntrs.send_length) == NULL)
2342 if (cYAML_create_number(stats, "recv_length",
2343 data.st_cntrs.recv_length) == NULL)
2346 if (cYAML_create_number(stats, "route_length",
2347 data.st_cntrs.route_length) == NULL)
2350 if (cYAML_create_number(stats, "drop_length",
2351 data.st_cntrs.drop_length) == NULL)
2354 if (show_rc == NULL)
2355 cYAML_print_tree(root);
2357 snprintf(err_str, sizeof(err_str), "\"success\"");
2358 rc = LUSTRE_CFG_RC_NO_ERR;
2360 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2361 cYAML_free_tree(root);
2362 } else if (show_rc != NULL && *show_rc != NULL) {
2363 cYAML_insert_sibling((*show_rc)->cy_child,
2370 cYAML_build_error(rc, seq_no, SHOW_CMD, "statistics", err_str, err_rc);
2375 typedef int (*cmd_handler_t)(struct cYAML *tree,
2376 struct cYAML **show_rc,
2377 struct cYAML **err_rc);
2379 static int handle_yaml_config_route(struct cYAML *tree, struct cYAML **show_rc,
2380 struct cYAML **err_rc)
2382 struct cYAML *net, *gw, *hop, *prio, *seq_no;
2384 net = cYAML_get_object_item(tree, "net");
2385 gw = cYAML_get_object_item(tree, "gateway");
2386 hop = cYAML_get_object_item(tree, "hop");
2387 prio = cYAML_get_object_item(tree, "priority");
2388 seq_no = cYAML_get_object_item(tree, "seq_no");
2390 return lustre_lnet_config_route((net) ? net->cy_valuestring : NULL,
2391 (gw) ? gw->cy_valuestring : NULL,
2392 (hop) ? hop->cy_valueint : -1,
2393 (prio) ? prio->cy_valueint : -1,
2394 (seq_no) ? seq_no->cy_valueint : -1,
2398 static void yaml_free_string_array(char **array, int num)
2401 char **sub_array = array;
2403 for (i = 0; i < num; i++) {
2404 if (*sub_array != NULL)
2414 * 0: <intf_name>['['<expr>']']
2415 * 1: <intf_name>['['<expr>']']
2417 static int yaml_copy_intf_info(struct cYAML *intf_tree,
2418 struct lnet_dlc_network_descr *nw_descr)
2420 struct cYAML *child = NULL;
2421 int intf_num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
2422 struct lnet_dlc_intf_descr *intf_descr, *tmp;
2424 if (intf_tree == NULL || nw_descr == NULL)
2425 return LUSTRE_CFG_RC_BAD_PARAM;
2427 /* now grab all the interfaces and their cpts */
2428 child = intf_tree->cy_child;
2429 while (child != NULL) {
2430 if (child->cy_valuestring == NULL) {
2431 child = child->cy_next;
2435 if (strlen(child->cy_valuestring) >= LNET_MAX_STR_LEN)
2438 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist,
2439 child->cy_valuestring,
2440 strlen(child->cy_valuestring));
2441 if (rc != LUSTRE_CFG_RC_NO_ERR)
2445 child = child->cy_next;
2449 return LUSTRE_CFG_RC_MISSING_PARAM;
2454 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
2456 list_del(&intf_descr->intf_on_network);
2457 free_intf_descr(intf_descr);
2464 yaml_extract_cmn_tunables(struct cYAML *tree,
2465 struct lnet_ioctl_config_lnd_cmn_tunables *tunables,
2466 struct cfs_expr_list **global_cpts)
2468 struct cYAML *tun, *item, *smp;
2471 tun = cYAML_get_object_item(tree, "tunables");
2473 item = cYAML_get_object_item(tun, "peer_timeout");
2475 tunables->lct_peer_timeout = item->cy_valueint;
2476 item = cYAML_get_object_item(tun, "peer_credits");
2478 tunables->lct_peer_tx_credits = item->cy_valueint;
2479 item = cYAML_get_object_item(tun, "peer_buffer_credits");
2481 tunables->lct_peer_rtr_credits = item->cy_valueint;
2482 item = cYAML_get_object_item(tun, "credits");
2484 tunables->lct_max_tx_credits = item->cy_valueint;
2485 smp = cYAML_get_object_item(tun, "CPT");
2487 rc = cfs_expr_list_parse(smp->cy_valuestring,
2488 strlen(smp->cy_valuestring),
2489 0, UINT_MAX, global_cpts);
2491 *global_cpts = NULL;
2501 yaml_extract_tunables(struct cYAML *tree,
2502 struct lnet_ioctl_config_lnd_tunables *tunables,
2503 struct cfs_expr_list **global_cpts,
2508 rc = yaml_extract_cmn_tunables(tree, &tunables->lt_cmn,
2514 lustre_yaml_extract_lnd_tunables(tree, net_type,
2522 * - net type: <net>[<NUM>]
2524 * - nid: <ip>@<net>[<NUM>]
2527 * 0: <intf_name>['['<expr>']']
2528 * 1: <intf_name>['['<expr>']']
2530 * peer_timeout: <NUM>
2531 * peer_credits: <NUM>
2532 * peer_buffer_credits: <NUM>
2535 * peercredits_hiw: <NUM>
2536 * map_on_demand: <NUM>
2537 * concurrent_sends: <NUM>
2538 * fmr_pool_size: <NUM>
2539 * fmr_flush_trigger: <NUM>
2542 * At least one interface is required. If no interfaces are provided the
2543 * network interface can not be configured.
2545 static int handle_yaml_config_ni(struct cYAML *tree, struct cYAML **show_rc,
2546 struct cYAML **err_rc)
2548 struct cYAML *net, *intf, *seq_no, *ip2net = NULL, *local_nis = NULL,
2550 int num_entries = 0, rc;
2551 struct lnet_dlc_network_descr nw_descr;
2552 struct cfs_expr_list *global_cpts = NULL;
2553 struct lnet_ioctl_config_lnd_tunables tunables;
2556 memset(&tunables, 0, sizeof(tunables));
2558 INIT_LIST_HEAD(&nw_descr.network_on_rule);
2559 INIT_LIST_HEAD(&nw_descr.nw_intflist);
2561 ip2net = cYAML_get_object_item(tree, "ip2net");
2562 net = cYAML_get_object_item(tree, "net type");
2564 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
2566 nw_descr.nw_id = LOLND;
2569 * if neither net nor ip2nets are present, then we can not
2570 * configure the network.
2572 if (!net && !ip2net)
2573 return LUSTRE_CFG_RC_MISSING_PARAM;
2575 local_nis = cYAML_get_object_item(tree, "local NI(s)");
2576 if (local_nis == NULL)
2577 return LUSTRE_CFG_RC_MISSING_PARAM;
2579 if (!cYAML_is_sequence(local_nis))
2580 return LUSTRE_CFG_RC_BAD_PARAM;
2582 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
2583 intf = cYAML_get_object_item(item, "interfaces");
2586 num_entries = yaml_copy_intf_info(intf, &nw_descr);
2587 if (num_entries <= 0) {
2588 cYAML_build_error(num_entries, -1, "ni", "add",
2589 "bad interface list",
2591 return LUSTRE_CFG_RC_BAD_PARAM;
2595 found = yaml_extract_tunables(tree, &tunables, &global_cpts,
2596 LNET_NETTYP(nw_descr.nw_id));
2597 seq_no = cYAML_get_object_item(tree, "seq_no");
2599 rc = lustre_lnet_config_ni(&nw_descr,
2601 (ip2net) ? ip2net->cy_valuestring : NULL,
2602 (found) ? &tunables: NULL,
2603 (seq_no) ? seq_no->cy_valueint : -1,
2606 if (global_cpts != NULL)
2607 cfs_expr_list_free(global_cpts);
2614 * - net-spec: <tcp|o2ib|gni>[NUM]
2616 * 0: <intf name>['['<expr>']']
2617 * 1: <intf name>['['<expr>']']
2619 * 0: <expr.expr.expr.expr>
2620 * 1: <expr.expr.expr.expr>
2622 static int handle_yaml_config_ip2nets(struct cYAML *tree,
2623 struct cYAML **show_rc,
2624 struct cYAML **err_rc)
2626 struct cYAML *net, *ip_range, *item = NULL, *intf = NULL,
2628 struct lustre_lnet_ip2nets ip2nets;
2629 struct lustre_lnet_ip_range_descr *ip_range_descr = NULL,
2631 int rc = LUSTRE_CFG_RC_NO_ERR;
2632 struct cfs_expr_list *global_cpts = NULL;
2633 struct cfs_expr_list *el, *el_tmp;
2634 struct lnet_ioctl_config_lnd_tunables tunables;
2635 struct lnet_dlc_intf_descr *intf_descr, *intf_tmp;
2638 memset(&tunables, 0, sizeof(tunables));
2640 /* initialize all lists */
2641 INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
2642 INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
2643 INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
2645 net = cYAML_get_object_item(tree, "net-spec");
2647 return LUSTRE_CFG_RC_BAD_PARAM;
2649 if (net != NULL && net->cy_valuestring == NULL)
2650 return LUSTRE_CFG_RC_BAD_PARAM;
2652 /* assign the network id */
2653 ip2nets.ip2nets_net.nw_id = libcfs_str2net(net->cy_valuestring);
2654 if (ip2nets.ip2nets_net.nw_id == LNET_NID_ANY)
2655 return LUSTRE_CFG_RC_BAD_PARAM;
2657 seq_no = cYAML_get_object_item(tree, "seq_no");
2659 intf = cYAML_get_object_item(tree, "interfaces");
2661 rc = yaml_copy_intf_info(intf, &ip2nets.ip2nets_net);
2663 return LUSTRE_CFG_RC_BAD_PARAM;
2666 ip_range = cYAML_get_object_item(tree, "ip-range");
2667 if (ip_range != NULL) {
2668 item = ip_range->cy_child;
2669 while (item != NULL) {
2670 if (item->cy_valuestring == NULL) {
2671 item = item->cy_next;
2675 rc = lustre_lnet_add_ip_range(&ip2nets.ip2nets_ip_ranges,
2676 item->cy_valuestring);
2678 if (rc != LUSTRE_CFG_RC_NO_ERR)
2681 item = item->cy_next;
2685 found = yaml_extract_tunables(tree, &tunables, &global_cpts,
2686 LNET_NETTYP(ip2nets.ip2nets_net.nw_id));
2688 rc = lustre_lnet_config_ip2nets(&ip2nets,
2689 (found) ? &tunables : NULL,
2691 (seq_no) ? seq_no->cy_valueint : -1,
2695 * don't stop because there was no match. Continue processing the
2696 * rest of the rules. If non-match then nothing is configured
2698 if (rc == LUSTRE_CFG_RC_NO_MATCH)
2699 rc = LUSTRE_CFG_RC_NO_ERR;
2701 list_for_each_entry_safe(intf_descr, intf_tmp,
2702 &ip2nets.ip2nets_net.nw_intflist,
2704 list_del(&intf_descr->intf_on_network);
2705 free_intf_descr(intf_descr);
2708 list_for_each_entry_safe(ip_range_descr, tmp,
2709 &ip2nets.ip2nets_ip_ranges,
2711 list_del(&ip_range_descr->ipr_entry);
2712 list_for_each_entry_safe(el, el_tmp, &ip_range_descr->ipr_expr,
2714 list_del(&el->el_link);
2715 cfs_expr_list_free(el);
2717 free(ip_range_descr);
2723 static int handle_yaml_del_ni(struct cYAML *tree, struct cYAML **show_rc,
2724 struct cYAML **err_rc)
2726 struct cYAML *net = NULL, *intf = NULL, *seq_no = NULL, *item = NULL,
2728 int num_entries, rc;
2729 struct lnet_dlc_network_descr nw_descr;
2731 INIT_LIST_HEAD(&nw_descr.network_on_rule);
2732 INIT_LIST_HEAD(&nw_descr.nw_intflist);
2734 net = cYAML_get_object_item(tree, "net type");
2736 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
2738 local_nis = cYAML_get_object_item(tree, "local NI(s)");
2739 if (local_nis == NULL)
2740 return LUSTRE_CFG_RC_MISSING_PARAM;
2742 if (!cYAML_is_sequence(local_nis))
2743 return LUSTRE_CFG_RC_BAD_PARAM;
2745 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
2746 intf = cYAML_get_object_item(item, "interfaces");
2749 num_entries = yaml_copy_intf_info(intf, &nw_descr);
2750 if (num_entries <= 0) {
2751 cYAML_build_error(num_entries, -1, "ni", "add",
2752 "bad interface list",
2754 return LUSTRE_CFG_RC_BAD_PARAM;
2758 seq_no = cYAML_get_object_item(tree, "seq_no");
2760 rc = lustre_lnet_del_ni((net) ? &nw_descr : NULL,
2761 (seq_no) ? seq_no->cy_valueint : -1,
2767 static int yaml_copy_peer_nids(struct cYAML *tree, char ***nidsppp)
2769 struct cYAML *nids_entry = NULL, *child = NULL, *entry = NULL;
2771 int num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
2773 nids_entry = cYAML_get_object_item(tree, "peer ni");
2774 if (cYAML_is_sequence(nids_entry)) {
2775 while (cYAML_get_next_seq_item(nids_entry, &child))
2780 return LUSTRE_CFG_RC_MISSING_PARAM;
2782 nids = calloc(sizeof(*nids) * num, 1);
2784 return LUSTRE_CFG_RC_OUT_OF_MEM;
2786 /* now grab all the nids */
2789 while (cYAML_get_next_seq_item(nids_entry, &child)) {
2790 entry = cYAML_get_object_item(child, "nid");
2793 nids[num] = calloc(strlen(entry->cy_valuestring) + 1, 1);
2795 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2798 strncpy(nids[num], entry->cy_valuestring,
2799 strlen(entry->cy_valuestring));
2809 yaml_free_string_array(nids, num);
2814 static int handle_yaml_config_peer(struct cYAML *tree, struct cYAML **show_rc,
2815 struct cYAML **err_rc)
2819 struct cYAML *seq_no, *prim_nid, *non_mr;
2821 num = yaml_copy_peer_nids(tree, &nids);
2825 seq_no = cYAML_get_object_item(tree, "seq_no");
2826 prim_nid = cYAML_get_object_item(tree, "primary nid");
2827 non_mr = cYAML_get_object_item(tree, "non_mr");
2829 rc = lustre_lnet_config_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
2831 (non_mr) ? false : true,
2832 (seq_no) ? seq_no->cy_valueint : -1,
2835 yaml_free_string_array(nids, num);
2839 static int handle_yaml_del_peer(struct cYAML *tree, struct cYAML **show_rc,
2840 struct cYAML **err_rc)
2844 struct cYAML *seq_no, *prim_nid;
2846 num = yaml_copy_peer_nids(tree, &nids);
2850 seq_no = cYAML_get_object_item(tree, "seq_no");
2851 prim_nid = cYAML_get_object_item(tree, "primary nid");
2853 rc = lustre_lnet_del_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
2855 (seq_no) ? seq_no->cy_valueint : -1,
2858 yaml_free_string_array(nids, num);
2862 static int handle_yaml_config_buffers(struct cYAML *tree,
2863 struct cYAML **show_rc,
2864 struct cYAML **err_rc)
2867 struct cYAML *tiny, *small, *large, *seq_no;
2869 tiny = cYAML_get_object_item(tree, "tiny");
2870 small = cYAML_get_object_item(tree, "small");
2871 large = cYAML_get_object_item(tree, "large");
2872 seq_no = cYAML_get_object_item(tree, "seq_no");
2874 rc = lustre_lnet_config_buffers((tiny) ? tiny->cy_valueint : -1,
2875 (small) ? small->cy_valueint : -1,
2876 (large) ? large->cy_valueint : -1,
2877 (seq_no) ? seq_no->cy_valueint : -1,
2883 static int handle_yaml_config_routing(struct cYAML *tree,
2884 struct cYAML **show_rc,
2885 struct cYAML **err_rc)
2887 int rc = LUSTRE_CFG_RC_NO_ERR;
2888 struct cYAML *seq_no, *enable;
2890 seq_no = cYAML_get_object_item(tree, "seq_no");
2891 enable = cYAML_get_object_item(tree, "enable");
2894 rc = lustre_lnet_enable_routing(enable->cy_valueint,
2896 seq_no->cy_valueint : -1,
2903 static int handle_yaml_del_route(struct cYAML *tree, struct cYAML **show_rc,
2904 struct cYAML **err_rc)
2908 struct cYAML *seq_no;
2910 net = cYAML_get_object_item(tree, "net");
2911 gw = cYAML_get_object_item(tree, "gateway");
2912 seq_no = cYAML_get_object_item(tree, "seq_no");
2914 return lustre_lnet_del_route((net) ? net->cy_valuestring : NULL,
2915 (gw) ? gw->cy_valuestring : NULL,
2916 (seq_no) ? seq_no->cy_valueint : -1,
2920 static int handle_yaml_del_routing(struct cYAML *tree, struct cYAML **show_rc,
2921 struct cYAML **err_rc)
2923 struct cYAML *seq_no;
2925 seq_no = cYAML_get_object_item(tree, "seq_no");
2927 return lustre_lnet_enable_routing(0, (seq_no) ?
2928 seq_no->cy_valueint : -1,
2932 static int handle_yaml_show_route(struct cYAML *tree, struct cYAML **show_rc,
2933 struct cYAML **err_rc)
2939 struct cYAML *detail;
2940 struct cYAML *seq_no;
2942 net = cYAML_get_object_item(tree, "net");
2943 gw = cYAML_get_object_item(tree, "gateway");
2944 hop = cYAML_get_object_item(tree, "hop");
2945 prio = cYAML_get_object_item(tree, "priority");
2946 detail = cYAML_get_object_item(tree, "detail");
2947 seq_no = cYAML_get_object_item(tree, "seq_no");
2949 return lustre_lnet_show_route((net) ? net->cy_valuestring : NULL,
2950 (gw) ? gw->cy_valuestring : NULL,
2951 (hop) ? hop->cy_valueint : -1,
2952 (prio) ? prio->cy_valueint : -1,
2953 (detail) ? detail->cy_valueint : 0,
2954 (seq_no) ? seq_no->cy_valueint : -1,
2959 static int handle_yaml_show_net(struct cYAML *tree, struct cYAML **show_rc,
2960 struct cYAML **err_rc)
2962 struct cYAML *net, *detail, *seq_no;
2964 net = cYAML_get_object_item(tree, "net");
2965 detail = cYAML_get_object_item(tree, "detail");
2966 seq_no = cYAML_get_object_item(tree, "seq_no");
2968 return lustre_lnet_show_net((net) ? net->cy_valuestring : NULL,
2969 (detail) ? detail->cy_valueint : 0,
2970 (seq_no) ? seq_no->cy_valueint : -1,
2975 static int handle_yaml_show_routing(struct cYAML *tree, struct cYAML **show_rc,
2976 struct cYAML **err_rc)
2978 struct cYAML *seq_no;
2980 seq_no = cYAML_get_object_item(tree, "seq_no");
2982 return lustre_lnet_show_routing((seq_no) ? seq_no->cy_valueint : -1,
2986 static int handle_yaml_show_peers(struct cYAML *tree, struct cYAML **show_rc,
2987 struct cYAML **err_rc)
2989 struct cYAML *seq_no, *nid, *detail;
2991 seq_no = cYAML_get_object_item(tree, "seq_no");
2992 detail = cYAML_get_object_item(tree, "detail");
2993 nid = cYAML_get_object_item(tree, "nid");
2995 return lustre_lnet_show_peer((nid) ? nid->cy_valuestring : NULL,
2996 (detail) ? detail->cy_valueint : 0,
2997 (seq_no) ? seq_no->cy_valueint : -1,
3001 static int handle_yaml_show_stats(struct cYAML *tree, struct cYAML **show_rc,
3002 struct cYAML **err_rc)
3004 struct cYAML *seq_no;
3006 seq_no = cYAML_get_object_item(tree, "seq_no");
3008 return lustre_lnet_show_stats((seq_no) ? seq_no->cy_valueint : -1,
3012 static int handle_yaml_config_numa(struct cYAML *tree, struct cYAML **show_rc,
3013 struct cYAML **err_rc)
3015 struct cYAML *seq_no, *range;
3017 seq_no = cYAML_get_object_item(tree, "seq_no");
3018 range = cYAML_get_object_item(tree, "range");
3020 return lustre_lnet_config_numa_range(range ? range->cy_valueint : -1,
3021 seq_no ? seq_no->cy_valueint : -1,
3025 static int handle_yaml_del_numa(struct cYAML *tree, struct cYAML **show_rc,
3026 struct cYAML **err_rc)
3028 struct cYAML *seq_no;
3030 seq_no = cYAML_get_object_item(tree, "seq_no");
3032 return lustre_lnet_config_numa_range(0, seq_no ? seq_no->cy_valueint : -1,
3036 static int handle_yaml_show_numa(struct cYAML *tree, struct cYAML **show_rc,
3037 struct cYAML **err_rc)
3039 struct cYAML *seq_no;
3041 seq_no = cYAML_get_object_item(tree, "seq_no");
3043 return lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint : -1,
3047 struct lookup_cmd_hdlr_tbl {
3052 static struct lookup_cmd_hdlr_tbl lookup_config_tbl[] = {
3053 { .name = "route", .cb = handle_yaml_config_route },
3054 { .name = "net", .cb = handle_yaml_config_ni },
3055 { .name = "ip2nets", .cb = handle_yaml_config_ip2nets },
3056 { .name = "peer", .cb = handle_yaml_config_peer },
3057 { .name = "routing", .cb = handle_yaml_config_routing },
3058 { .name = "buffers", .cb = handle_yaml_config_buffers },
3059 { .name = "numa", .cb = handle_yaml_config_numa },
3062 static struct lookup_cmd_hdlr_tbl lookup_del_tbl[] = {
3063 { .name = "route", .cb = handle_yaml_del_route },
3064 { .name = "net", .cb = handle_yaml_del_ni },
3065 { .name = "peer", .cb = handle_yaml_del_peer },
3066 { .name = "routing", .cb = handle_yaml_del_routing },
3067 { .name = "numa", .cb = handle_yaml_del_numa },
3070 static struct lookup_cmd_hdlr_tbl lookup_show_tbl[] = {
3071 { .name = "route", .cb = handle_yaml_show_route },
3072 { .name = "net", .cb = handle_yaml_show_net },
3073 { .name = "buffers", .cb = handle_yaml_show_routing },
3074 { .name = "routing", .cb = handle_yaml_show_routing },
3075 { .name = "peer", .cb = handle_yaml_show_peers },
3076 { .name = "statistics", .cb = handle_yaml_show_stats },
3077 { .name = "numa", .cb = handle_yaml_show_numa },
3080 static cmd_handler_t lookup_fn(char *key,
3081 struct lookup_cmd_hdlr_tbl *tbl)
3087 for (i = 0; tbl[i].name != NULL; i++) {
3088 if (strncmp(key, tbl[i].name, strlen(tbl[i].name)) == 0)
3095 static int lustre_yaml_cb_helper(char *f, struct lookup_cmd_hdlr_tbl *table,
3096 struct cYAML **show_rc, struct cYAML **err_rc)
3098 struct cYAML *tree, *item = NULL, *head, *child;
3100 char err_str[LNET_MAX_STR_LEN];
3101 int rc = LUSTRE_CFG_RC_NO_ERR, return_rc = LUSTRE_CFG_RC_NO_ERR;
3103 tree = cYAML_build_tree(f, NULL, 0, err_rc, false);
3105 return LUSTRE_CFG_RC_BAD_PARAM;
3107 child = tree->cy_child;
3108 while (child != NULL) {
3109 cb = lookup_fn(child->cy_string, table);
3111 snprintf(err_str, sizeof(err_str),
3112 "\"call back for '%s' not found\"",
3114 cYAML_build_error(LUSTRE_CFG_RC_BAD_PARAM, -1,
3115 "yaml", "helper", err_str, err_rc);
3119 if (cYAML_is_sequence(child)) {
3120 while ((head = cYAML_get_next_seq_item(child, &item))
3122 rc = cb(head, show_rc, err_rc);
3123 if (rc != LUSTRE_CFG_RC_NO_ERR)
3127 rc = cb(child, show_rc, err_rc);
3128 if (rc != LUSTRE_CFG_RC_NO_ERR)
3132 child = child->cy_next;
3136 cYAML_free_tree(tree);
3141 int lustre_yaml_config(char *f, struct cYAML **err_rc)
3143 return lustre_yaml_cb_helper(f, lookup_config_tbl,
3147 int lustre_yaml_del(char *f, struct cYAML **err_rc)
3149 return lustre_yaml_cb_helper(f, lookup_del_tbl,
3153 int lustre_yaml_show(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
3155 return lustre_yaml_cb_helper(f, lookup_show_tbl,