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;
385 if (!nids || num_nids == 0) {
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 (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 (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 (tunables != NULL)
1201 memcpy(tun, tunables, sizeof(*tunables));
1203 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1208 "\"cannot add network: %s\"", strerror(errno));
1216 return LUSTRE_CFG_RC_NO_ERR;
1220 lustre_lnet_config_ip2nets(struct lustre_lnet_ip2nets *ip2nets,
1221 struct lnet_ioctl_config_lnd_tunables *tunables,
1222 struct cfs_expr_list *global_cpts,
1223 int seq_no, struct cYAML **err_rc)
1225 lnet_nid_t *nids = NULL;
1228 char err_str[LNET_MAX_STR_LEN];
1230 snprintf(err_str, sizeof(err_str), "\"success\"");
1235 "\"incomplete ip2nets information\"");
1236 rc = LUSTRE_CFG_RC_BAD_PARAM;
1241 * call below function to resolve the rules into a list of nids.
1242 * The memory is allocated in that function then freed here when
1243 * it's no longer needed.
1245 rc = lustre_lnet_resolve_ip2nets_rule(ip2nets, &nids, &nnids);
1246 if (rc != LUSTRE_CFG_RC_NO_ERR && rc != LUSTRE_CFG_RC_MATCH) {
1249 "\"cannot resolve ip2nets rule\"");
1253 if (list_empty(&ip2nets->ip2nets_net.nw_intflist)) {
1254 snprintf(err_str, sizeof(err_str),
1255 "\"no interfaces match ip2nets rules\"");
1259 rc = lustre_lnet_ioctl_config_ni(&ip2nets->ip2nets_net.nw_intflist,
1260 tunables, global_cpts, nids,
1267 cYAML_build_error(rc, seq_no, ADD_CMD, "ip2nets", err_str, err_rc);
1271 int lustre_lnet_config_ni(struct lnet_dlc_network_descr *nw_descr,
1272 struct cfs_expr_list *global_cpts,
1274 struct lnet_ioctl_config_lnd_tunables *tunables,
1275 int seq_no, struct cYAML **err_rc)
1278 struct lnet_ioctl_config_ni *conf;
1279 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1280 char buf[LNET_MAX_STR_LEN];
1281 int rc = LUSTRE_CFG_RC_NO_ERR;
1282 char err_str[LNET_MAX_STR_LEN];
1283 lnet_nid_t *nids = NULL;
1287 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1290 snprintf(err_str, sizeof(err_str), "\"success\"");
1292 if (ip2net == NULL && nw_descr == NULL) {
1295 "\"mandatory parameters not specified.\"");
1296 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1300 if (ip2net != NULL && strlen(ip2net) >= sizeof(buf)) {
1303 "\"ip2net string too long %d\"",
1304 (int)strlen(ip2net));
1305 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1309 if (ip2net != NULL) {
1310 if (tunables != NULL)
1311 len = sizeof(struct lnet_ioctl_config_ni) +
1312 sizeof(struct lnet_ioctl_config_lnd_tunables);
1314 len = sizeof(struct lnet_ioctl_config_ni);
1315 data = calloc(1, len);
1317 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1320 conf = (struct lnet_ioctl_config_ni*) data;
1321 if (tunables != NULL)
1322 tun = (struct lnet_ioctl_config_lnd_tunables*)
1323 (data + sizeof(*conf));
1325 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1326 conf->lic_cfg_hdr.ioc_len = len;
1327 strncpy(conf->lic_legacy_ip2nets, ip2net,
1330 if (global_cpts != NULL) {
1331 count = cfs_expr_list_values(global_cpts,
1332 LNET_MAX_SHOW_NUM_CPT,
1335 memcpy(conf->lic_cpts, cpt_array,
1336 sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1345 conf->lic_ncpts = count;
1347 if (tunables != NULL)
1348 memcpy(tun, tunables, sizeof(*tunables));
1350 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1355 "\"cannot add network: %s\"", strerror(errno));
1362 if (LNET_NETTYP(nw_descr->nw_id) == LOLND) {
1363 rc = LUSTRE_CFG_RC_NO_ERR;
1367 if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1370 "\"cannot parse net '%s'\"",
1371 libcfs_net2str(nw_descr->nw_id));
1372 rc = LUSTRE_CFG_RC_BAD_PARAM;
1376 if (list_empty(&nw_descr->nw_intflist)) {
1379 "\"no interface name provided\"");
1380 rc = LUSTRE_CFG_RC_BAD_PARAM;
1384 rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids);
1386 snprintf(err_str, sizeof(err_str),
1387 "\"bad parameter\"");
1388 rc = LUSTRE_CFG_RC_BAD_PARAM;
1392 rc = lustre_lnet_ioctl_config_ni(&nw_descr->nw_intflist,
1393 tunables, global_cpts, nids,
1397 if (nw_descr != NULL) {
1398 list_for_each_entry_safe(intf_descr, tmp,
1399 &nw_descr->nw_intflist,
1401 list_del(&intf_descr->intf_on_network);
1402 free_intf_descr(intf_descr);
1406 cYAML_build_error(rc, seq_no, ADD_CMD, "net", err_str, err_rc);
1417 int lustre_lnet_del_ni(struct lnet_dlc_network_descr *nw_descr,
1418 int seq_no, struct cYAML **err_rc)
1420 struct lnet_ioctl_config_ni data;
1421 int rc = LUSTRE_CFG_RC_NO_ERR, i;
1422 char err_str[LNET_MAX_STR_LEN];
1423 lnet_nid_t *nids = NULL;
1425 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1427 snprintf(err_str, sizeof(err_str), "\"success\"");
1429 if (nw_descr == NULL) {
1432 "\"missing mandatory parameter\"");
1433 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1437 if (LNET_NETTYP(nw_descr->nw_id) == LOLND)
1438 return LUSTRE_CFG_RC_NO_ERR;
1440 if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1443 "\"cannot parse net '%s'\"",
1444 libcfs_net2str(nw_descr->nw_id));
1445 rc = LUSTRE_CFG_RC_BAD_PARAM;
1449 rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids);
1451 snprintf(err_str, sizeof(err_str),
1452 "\"bad parameter\"");
1453 rc = LUSTRE_CFG_RC_BAD_PARAM;
1458 * no interfaces just the nw_id is specified
1461 nids = calloc(1, sizeof(*nids));
1463 snprintf(err_str, sizeof(err_str),
1464 "\"out of memory\"");
1465 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1468 nids[0] = LNET_MKNID(nw_descr->nw_id, 0);
1472 for (i = 0; i < nnids; i++) {
1473 LIBCFS_IOC_INIT_V2(data, lic_cfg_hdr);
1474 data.lic_nid = nids[i];
1476 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_LOCAL_NI, &data);
1481 "\"cannot del network: %s\"", strerror(errno));
1485 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
1487 list_del(&intf_descr->intf_on_network);
1488 free_intf_descr(intf_descr);
1492 cYAML_build_error(rc, seq_no, DEL_CMD, "net", err_str, err_rc);
1500 int lustre_lnet_show_net(char *nw, int detail, int seq_no,
1501 struct cYAML **show_rc, struct cYAML **err_rc)
1504 struct lnet_ioctl_config_ni *ni_data;
1505 struct lnet_ioctl_config_lnd_tunables *lnd;
1506 struct lnet_ioctl_element_stats *stats;
1507 __u32 net = LNET_NIDNET(LNET_NID_ANY);
1508 __u32 prev_net = LNET_NIDNET(LNET_NID_ANY);
1509 int rc = LUSTRE_CFG_RC_OUT_OF_MEM, i, j;
1511 struct cYAML *root = NULL, *tunables = NULL,
1512 *net_node = NULL, *interfaces = NULL,
1513 *item = NULL, *first_seq = NULL,
1514 *tmp = NULL, *statistics = NULL;
1515 int str_buf_len = LNET_MAX_SHOW_NUM_CPT * 2;
1516 char str_buf[str_buf_len];
1518 char err_str[LNET_MAX_STR_LEN];
1519 bool exist = false, new_net = true;
1521 size_t buf_size = sizeof(*ni_data) + sizeof(*lnd) + sizeof(*stats);
1523 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1525 buf = calloc(1, buf_size);
1529 ni_data = (struct lnet_ioctl_config_ni *)buf;
1532 net = libcfs_str2net(nw);
1533 if (net == LNET_NIDNET(LNET_NID_ANY)) {
1536 "\"cannot parse net '%s'\"", nw);
1537 rc = LUSTRE_CFG_RC_BAD_PARAM;
1542 root = cYAML_create_object(NULL, NULL);
1546 net_node = cYAML_create_seq(root, "net");
1547 if (net_node == NULL)
1554 memset(buf, 0, buf_size);
1556 LIBCFS_IOC_INIT_V2(*ni_data, lic_cfg_hdr);
1558 * set the ioc_len to the proper value since INIT assumes
1561 ni_data->lic_cfg_hdr.ioc_len = buf_size;
1562 ni_data->lic_idx = i;
1564 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LOCAL_NI, ni_data);
1570 rc_net = LNET_NIDNET(ni_data->lic_nid);
1572 /* filter on provided data */
1573 if (net != LNET_NIDNET(LNET_NID_ANY) &&
1577 /* default rc to -1 in case we hit the goto */
1581 stats = (struct lnet_ioctl_element_stats *)ni_data->lic_bulk;
1582 lnd = (struct lnet_ioctl_config_lnd_tunables *)
1583 (ni_data->lic_bulk + sizeof(*stats));
1585 if (rc_net != prev_net) {
1592 if (!cYAML_create_string(net_node, "net type",
1593 libcfs_net2str(rc_net)))
1596 tmp = cYAML_create_seq(net_node, "local NI(s)");
1602 /* create the tree to be printed. */
1603 item = cYAML_create_seq_item(tmp);
1607 if (first_seq == NULL)
1610 if (cYAML_create_string(item, "nid",
1611 libcfs_nid2str(ni_data->lic_nid)) == NULL)
1614 if (cYAML_create_string(item,
1616 (ni_data->lic_status ==
1617 LNET_NI_STATUS_UP) ?
1618 "up" : "down") == NULL)
1621 /* don't add interfaces unless there is at least one
1623 if (strlen(ni_data->lic_ni_intf[0]) > 0) {
1624 interfaces = cYAML_create_object(item, "interfaces");
1625 if (interfaces == NULL)
1628 for (j = 0; j < LNET_NUM_INTERFACES; j++) {
1629 if (strlen(ni_data->lic_ni_intf[j]) > 0) {
1631 sizeof(str_buf), "%d", j);
1632 if (cYAML_create_string(interfaces,
1634 ni_data->lic_ni_intf[j]) ==
1644 statistics = cYAML_create_object(item, "statistics");
1645 if (statistics == NULL)
1648 if (cYAML_create_number(statistics, "send_count",
1649 stats->iel_send_count)
1653 if (cYAML_create_number(statistics, "recv_count",
1654 stats->iel_recv_count)
1658 if (cYAML_create_number(statistics, "drop_count",
1659 stats->iel_drop_count)
1663 tunables = cYAML_create_object(item, "tunables");
1667 rc = lustre_net_show_tunables(tunables, &lnd->lt_cmn);
1668 if (rc != LUSTRE_CFG_RC_NO_ERR)
1671 tunables = cYAML_create_object(item, "lnd tunables");
1672 if (tunables == NULL)
1675 rc = lustre_ni_show_tunables(tunables, LNET_NETTYP(rc_net),
1677 if (rc != LUSTRE_CFG_RC_NO_ERR)
1680 if (cYAML_create_number(item, "tcp bonding",
1681 ni_data->lic_tcp_bonding)
1685 if (cYAML_create_number(item, "dev cpt",
1686 ni_data->lic_dev_cpt) == NULL)
1689 /* out put the CPTs in the format: "[x,x,x,...]" */
1690 limit = str_buf + str_buf_len - 3;
1691 pos += snprintf(pos, limit - pos, "\"[");
1692 for (j = 0 ; ni_data->lic_ncpts >= 1 &&
1693 j < ni_data->lic_ncpts &&
1695 pos += snprintf(pos, limit - pos,
1696 "%d", ni_data->lic_cpts[j]);
1697 if ((j + 1) < ni_data->lic_ncpts)
1698 pos += snprintf(pos, limit - pos, ",");
1700 pos += snprintf(pos, 3, "]\"");
1702 if (ni_data->lic_ncpts >= 1 &&
1703 cYAML_create_string(item, "CPT",
1709 /* Print out the net information only if show_rc is not provided */
1710 if (show_rc == NULL)
1711 cYAML_print_tree(root);
1713 if (l_errno != ENOENT) {
1716 "\"cannot get networks: %s\"",
1721 rc = LUSTRE_CFG_RC_NO_ERR;
1723 snprintf(err_str, sizeof(err_str), "\"success\"");
1725 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
1726 cYAML_free_tree(root);
1727 } else if (show_rc != NULL && *show_rc != NULL) {
1728 struct cYAML *show_node;
1729 /* find the net node, if one doesn't exist
1730 * then insert one. Otherwise add to the one there
1732 show_node = cYAML_get_object_item(*show_rc, "net");
1733 if (show_node != NULL && cYAML_is_sequence(show_node)) {
1734 cYAML_insert_child(show_node, first_seq);
1737 } else if (show_node == NULL) {
1738 cYAML_insert_sibling((*show_rc)->cy_child,
1742 cYAML_free_tree(root);
1748 cYAML_build_error(rc, seq_no, SHOW_CMD, "net", err_str, err_rc);
1753 int lustre_lnet_enable_routing(int enable, int seq_no, struct cYAML **err_rc)
1755 struct lnet_ioctl_config_data data;
1756 int rc = LUSTRE_CFG_RC_NO_ERR;
1757 char err_str[LNET_MAX_STR_LEN];
1759 snprintf(err_str, sizeof(err_str), "\"success\"");
1761 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1762 data.cfg_config_u.cfg_buffers.buf_enable = (enable) ? 1 : 0;
1764 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIG_RTR, &data);
1769 "\"cannot %s routing %s\"",
1770 (enable) ? "enable" : "disable", strerror(errno));
1775 cYAML_build_error(rc, seq_no,
1776 (enable) ? ADD_CMD : DEL_CMD,
1777 "routing", err_str, err_rc);
1782 int lustre_lnet_config_numa_range(int range, int seq_no, struct cYAML **err_rc)
1784 struct lnet_ioctl_numa_range data;
1785 int rc = LUSTRE_CFG_RC_NO_ERR;
1786 char err_str[LNET_MAX_STR_LEN];
1788 snprintf(err_str, sizeof(err_str), "\"success\"");
1793 "\"range must be >= 0\"");
1794 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1798 LIBCFS_IOC_INIT_V2(data, nr_hdr);
1799 data.nr_range = range;
1801 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_SET_NUMA_RANGE, &data);
1806 "\"cannot configure buffers: %s\"", strerror(errno));
1811 cYAML_build_error(rc, seq_no, ADD_CMD, "numa_range", err_str, err_rc);
1816 int lustre_lnet_config_buffers(int tiny, int small, int large, int seq_no,
1817 struct cYAML **err_rc)
1819 struct lnet_ioctl_config_data data;
1820 int rc = LUSTRE_CFG_RC_NO_ERR;
1821 char err_str[LNET_MAX_STR_LEN];
1823 snprintf(err_str, sizeof(err_str), "\"success\"");
1825 /* -1 indicates to ignore changes to this field */
1826 if (tiny < -1 || small < -1 || large < -1) {
1829 "\"tiny, small and large must be >= 0\"");
1830 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1834 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1835 data.cfg_config_u.cfg_buffers.buf_tiny = tiny;
1836 data.cfg_config_u.cfg_buffers.buf_small = small;
1837 data.cfg_config_u.cfg_buffers.buf_large = large;
1839 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_BUF, &data);
1844 "\"cannot configure buffers: %s\"", strerror(errno));
1849 cYAML_build_error(rc, seq_no, ADD_CMD, "buf", err_str, err_rc);
1854 int lustre_lnet_show_routing(int seq_no, struct cYAML **show_rc,
1855 struct cYAML **err_rc)
1857 struct lnet_ioctl_config_data *data;
1858 struct lnet_ioctl_pool_cfg *pool_cfg = NULL;
1859 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1862 char *pools[LNET_NRBPOOLS] = {"tiny", "small", "large"};
1863 int buf_count[LNET_NRBPOOLS] = {0};
1864 struct cYAML *root = NULL, *pools_node = NULL,
1865 *type_node = NULL, *item = NULL, *cpt = NULL,
1866 *first_seq = NULL, *buffers = NULL;
1868 char err_str[LNET_MAX_STR_LEN];
1869 char node_name[LNET_MAX_STR_LEN];
1872 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1874 buf = calloc(1, sizeof(*data) + sizeof(*pool_cfg));
1878 data = (struct lnet_ioctl_config_data *)buf;
1880 root = cYAML_create_object(NULL, NULL);
1884 pools_node = cYAML_create_seq(root, "routing");
1885 if (pools_node == NULL)
1889 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
1890 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
1891 sizeof(struct lnet_ioctl_pool_cfg);
1892 data->cfg_count = i;
1894 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_BUF, data);
1902 pool_cfg = (struct lnet_ioctl_pool_cfg *)data->cfg_bulk;
1904 snprintf(node_name, sizeof(node_name), "cpt[%d]", i);
1905 item = cYAML_create_seq_item(pools_node);
1909 if (first_seq == NULL)
1912 cpt = cYAML_create_object(item, node_name);
1916 /* create the tree and print */
1917 for (j = 0; j < LNET_NRBPOOLS; j++) {
1918 type_node = cYAML_create_object(cpt, pools[j]);
1919 if (type_node == NULL)
1921 if (cYAML_create_number(type_node, "npages",
1922 pool_cfg->pl_pools[j].pl_npages)
1925 if (cYAML_create_number(type_node, "nbuffers",
1926 pool_cfg->pl_pools[j].
1927 pl_nbuffers) == NULL)
1929 if (cYAML_create_number(type_node, "credits",
1930 pool_cfg->pl_pools[j].
1931 pl_credits) == NULL)
1933 if (cYAML_create_number(type_node, "mincredits",
1934 pool_cfg->pl_pools[j].
1935 pl_mincredits) == NULL)
1937 /* keep track of the total count for each of the
1938 * tiny, small and large buffers */
1939 buf_count[j] += pool_cfg->pl_pools[j].pl_nbuffers;
1943 if (pool_cfg != NULL) {
1944 item = cYAML_create_seq_item(pools_node);
1948 if (cYAML_create_number(item, "enable", pool_cfg->pl_routing) ==
1953 /* create a buffers entry in the show. This is necessary so that
1954 * if the YAML output is used to configure a node, the buffer
1955 * configuration takes hold */
1956 buffers = cYAML_create_object(root, "buffers");
1957 if (buffers == NULL)
1960 for (i = 0; i < LNET_NRBPOOLS; i++) {
1961 if (cYAML_create_number(buffers, pools[i], buf_count[i]) == NULL)
1965 if (show_rc == NULL)
1966 cYAML_print_tree(root);
1968 if (l_errno != ENOENT) {
1971 "\"cannot get routing information: %s\"",
1976 rc = LUSTRE_CFG_RC_NO_ERR;
1978 snprintf(err_str, sizeof(err_str), "\"success\"");
1979 rc = LUSTRE_CFG_RC_NO_ERR;
1983 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
1984 cYAML_free_tree(root);
1985 } else if (show_rc != NULL && *show_rc != NULL) {
1986 struct cYAML *routing_node;
1987 /* there should exist only one routing block and one
1988 * buffers block. If there already exists a previous one
1989 * then don't add another */
1990 routing_node = cYAML_get_object_item(*show_rc, "routing");
1991 if (routing_node == NULL) {
1992 cYAML_insert_sibling((*show_rc)->cy_child,
1996 cYAML_free_tree(root);
2002 cYAML_build_error(rc, seq_no, SHOW_CMD, "routing", err_str, err_rc);
2007 int lustre_lnet_show_peer(char *knid, int detail, int seq_no,
2008 struct cYAML **show_rc, struct cYAML **err_rc)
2011 * TODO: This function is changing in a future patch to accommodate
2012 * PEER_LIST and proper filtering on any nid of the peer
2014 struct lnet_ioctl_peer_cfg peer_info;
2015 struct lnet_peer_ni_credit_info *lpni_cri;
2016 struct lnet_ioctl_element_stats *lpni_stats;
2017 int rc = LUSTRE_CFG_RC_OUT_OF_MEM, ncpt = 0, i = 0, j = 0;
2019 struct cYAML *root = NULL, *peer = NULL, *peer_ni = NULL,
2020 *first_seq = NULL, *peer_root = NULL, *tmp = NULL;
2021 char err_str[LNET_MAX_STR_LEN];
2022 lnet_nid_t prev_primary_nid = LNET_NID_ANY, primary_nid = LNET_NID_ANY;
2023 int data_size = sizeof(*lpni_cri) + sizeof(*lpni_stats);
2024 char *data = malloc(data_size);
2025 bool new_peer = true;
2027 snprintf(err_str, sizeof(err_str),
2028 "\"out of memory\"");
2033 /* create struct cYAML root object */
2034 root = cYAML_create_object(NULL, NULL);
2038 peer_root = cYAML_create_seq(root, "peer");
2039 if (peer_root == NULL)
2043 primary_nid = libcfs_str2nid(knid);
2047 memset(data, 0, data_size);
2048 memset(&peer_info, 0, sizeof(peer_info));
2049 LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2050 peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2051 peer_info.prcfg_count = i;
2052 peer_info.prcfg_bulk = (void *)data;
2053 peer_info.prcfg_size = data_size;
2055 rc = l_ioctl(LNET_DEV_ID,
2056 IOC_LIBCFS_GET_PEER_NI, &peer_info);
2062 if (primary_nid != LNET_NID_ANY &&
2063 primary_nid != peer_info.prcfg_prim_nid)
2066 lpni_cri = peer_info.prcfg_bulk;
2067 lpni_stats = peer_info.prcfg_bulk + sizeof(*lpni_cri);
2069 peer = cYAML_create_seq_item(peer_root);
2073 if (peer_info.prcfg_prim_nid != prev_primary_nid) {
2074 prev_primary_nid = peer_info.prcfg_prim_nid;
2079 lnet_nid_t pnid = peer_info.prcfg_prim_nid;
2080 if (cYAML_create_string(peer, "primary nid",
2081 libcfs_nid2str(pnid))
2084 if (cYAML_create_string(peer, "Multi-Rail",
2085 peer_info.prcfg_mr ?
2089 tmp = cYAML_create_seq(peer, "peer ni");
2095 if (first_seq == NULL)
2098 peer_ni = cYAML_create_seq_item(tmp);
2099 if (peer_ni == NULL)
2102 if (cYAML_create_string(peer_ni, "nid",
2104 (peer_info.prcfg_cfg_nid))
2108 if (cYAML_create_string(peer_ni, "state",
2109 lpni_cri->cr_aliveness)
2116 if (cYAML_create_number(peer_ni, "max_ni_tx_credits",
2117 lpni_cri->cr_ni_peer_tx_credits)
2121 if (cYAML_create_number(peer_ni, "available_tx_credits",
2122 lpni_cri->cr_peer_tx_credits)
2126 if (cYAML_create_number(peer_ni, "min_tx_credits",
2127 lpni_cri->cr_peer_min_tx_credits)
2131 if (cYAML_create_number(peer_ni, "tx_q_num_of_buf",
2132 lpni_cri->cr_peer_tx_qnob)
2136 if (cYAML_create_number(peer_ni, "available_rtr_credits",
2137 lpni_cri->cr_peer_rtr_credits)
2141 if (cYAML_create_number(peer_ni, "min_rtr_credits",
2142 lpni_cri->cr_peer_min_rtr_credits)
2146 if (cYAML_create_number(peer_ni, "send_count",
2147 lpni_stats->iel_send_count)
2151 if (cYAML_create_number(peer_ni, "recv_count",
2152 lpni_stats->iel_recv_count)
2156 if (cYAML_create_number(peer_ni, "drop_count",
2157 lpni_stats->iel_drop_count)
2161 if (cYAML_create_number(peer_ni, "refcount",
2162 lpni_cri->cr_refcount) == NULL)
2166 if (l_errno != ENOENT) {
2169 "\"cannot get peer information: %s\"",
2178 /* print output iff show_rc is not provided */
2179 if (show_rc == NULL)
2180 cYAML_print_tree(root);
2182 snprintf(err_str, sizeof(err_str), "\"success\"");
2183 rc = LUSTRE_CFG_RC_NO_ERR;
2186 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2187 cYAML_free_tree(root);
2188 } else if (show_rc != NULL && *show_rc != NULL) {
2189 struct cYAML *show_node;
2190 /* find the peer node, if one doesn't exist then
2191 * insert one. Otherwise add to the one there
2193 show_node = cYAML_get_object_item(*show_rc,
2195 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2196 cYAML_insert_child(show_node, first_seq);
2199 } else if (show_node == NULL) {
2200 cYAML_insert_sibling((*show_rc)->cy_child,
2204 cYAML_free_tree(root);
2210 cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
2216 int lustre_lnet_show_numa_range(int seq_no, struct cYAML **show_rc,
2217 struct cYAML **err_rc)
2219 struct lnet_ioctl_numa_range data;
2222 char err_str[LNET_MAX_STR_LEN];
2223 struct cYAML *root = NULL, *range = NULL;
2225 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2227 LIBCFS_IOC_INIT_V2(data, nr_hdr);
2229 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NUMA_RANGE, &data);
2234 "\"cannot get numa range: %s\"",
2240 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2242 root = cYAML_create_object(NULL, NULL);
2246 range = cYAML_create_object(root, "numa");
2250 if (cYAML_create_number(range, "range",
2251 data.nr_range) == NULL)
2254 if (show_rc == NULL)
2255 cYAML_print_tree(root);
2257 snprintf(err_str, sizeof(err_str), "\"success\"");
2258 rc = LUSTRE_CFG_RC_NO_ERR;
2260 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2261 cYAML_free_tree(root);
2262 } else if (show_rc != NULL && *show_rc != NULL) {
2263 cYAML_insert_sibling((*show_rc)->cy_child,
2270 cYAML_build_error(rc, seq_no, SHOW_CMD, "numa", err_str, err_rc);
2275 int lustre_lnet_show_stats(int seq_no, struct cYAML **show_rc,
2276 struct cYAML **err_rc)
2278 struct lnet_ioctl_lnet_stats data;
2281 char err_str[LNET_MAX_STR_LEN];
2282 struct cYAML *root = NULL, *stats = NULL;
2284 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2286 LIBCFS_IOC_INIT_V2(data, st_hdr);
2288 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LNET_STATS, &data);
2293 "\"cannot get lnet statistics: %s\"",
2299 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2301 root = cYAML_create_object(NULL, NULL);
2305 stats = cYAML_create_object(root, "statistics");
2309 if (cYAML_create_number(stats, "msgs_alloc",
2310 data.st_cntrs.msgs_alloc) == NULL)
2313 if (cYAML_create_number(stats, "msgs_max",
2314 data.st_cntrs.msgs_max) == NULL)
2317 if (cYAML_create_number(stats, "errors",
2318 data.st_cntrs.errors) == NULL)
2321 if (cYAML_create_number(stats, "send_count",
2322 data.st_cntrs.send_count) == NULL)
2325 if (cYAML_create_number(stats, "recv_count",
2326 data.st_cntrs.recv_count) == NULL)
2329 if (cYAML_create_number(stats, "route_count",
2330 data.st_cntrs.route_count) == NULL)
2333 if (cYAML_create_number(stats, "drop_count",
2334 data.st_cntrs.drop_count) == NULL)
2337 if (cYAML_create_number(stats, "send_length",
2338 data.st_cntrs.send_length) == NULL)
2341 if (cYAML_create_number(stats, "recv_length",
2342 data.st_cntrs.recv_length) == NULL)
2345 if (cYAML_create_number(stats, "route_length",
2346 data.st_cntrs.route_length) == NULL)
2349 if (cYAML_create_number(stats, "drop_length",
2350 data.st_cntrs.drop_length) == NULL)
2353 if (show_rc == NULL)
2354 cYAML_print_tree(root);
2356 snprintf(err_str, sizeof(err_str), "\"success\"");
2357 rc = LUSTRE_CFG_RC_NO_ERR;
2359 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2360 cYAML_free_tree(root);
2361 } else if (show_rc != NULL && *show_rc != NULL) {
2362 cYAML_insert_sibling((*show_rc)->cy_child,
2369 cYAML_build_error(rc, seq_no, SHOW_CMD, "statistics", err_str, err_rc);
2374 typedef int (*cmd_handler_t)(struct cYAML *tree,
2375 struct cYAML **show_rc,
2376 struct cYAML **err_rc);
2378 static int handle_yaml_config_route(struct cYAML *tree, struct cYAML **show_rc,
2379 struct cYAML **err_rc)
2381 struct cYAML *net, *gw, *hop, *prio, *seq_no;
2383 net = cYAML_get_object_item(tree, "net");
2384 gw = cYAML_get_object_item(tree, "gateway");
2385 hop = cYAML_get_object_item(tree, "hop");
2386 prio = cYAML_get_object_item(tree, "priority");
2387 seq_no = cYAML_get_object_item(tree, "seq_no");
2389 return lustre_lnet_config_route((net) ? net->cy_valuestring : NULL,
2390 (gw) ? gw->cy_valuestring : NULL,
2391 (hop) ? hop->cy_valueint : -1,
2392 (prio) ? prio->cy_valueint : -1,
2393 (seq_no) ? seq_no->cy_valueint : -1,
2397 static void yaml_free_string_array(char **array, int num)
2400 char **sub_array = array;
2402 for (i = 0; i < num; i++) {
2403 if (*sub_array != NULL)
2413 * 0: <intf_name>['['<expr>']']
2414 * 1: <intf_name>['['<expr>']']
2416 static int yaml_copy_intf_info(struct cYAML *intf_tree,
2417 struct lnet_dlc_network_descr *nw_descr)
2419 struct cYAML *child = NULL;
2420 int intf_num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
2421 struct lnet_dlc_intf_descr *intf_descr, *tmp;
2423 if (intf_tree == NULL || nw_descr == NULL)
2424 return LUSTRE_CFG_RC_BAD_PARAM;
2426 /* now grab all the interfaces and their cpts */
2427 child = intf_tree->cy_child;
2428 while (child != NULL) {
2429 if (child->cy_valuestring == NULL) {
2430 child = child->cy_next;
2434 if (strlen(child->cy_valuestring) >= LNET_MAX_STR_LEN)
2437 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist,
2438 child->cy_valuestring,
2439 strlen(child->cy_valuestring));
2440 if (rc != LUSTRE_CFG_RC_NO_ERR)
2444 child = child->cy_next;
2448 return LUSTRE_CFG_RC_MISSING_PARAM;
2453 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
2455 list_del(&intf_descr->intf_on_network);
2456 free_intf_descr(intf_descr);
2463 yaml_extract_cmn_tunables(struct cYAML *tree,
2464 struct lnet_ioctl_config_lnd_cmn_tunables *tunables,
2465 struct cfs_expr_list **global_cpts)
2467 struct cYAML *tun, *item, *smp;
2470 tun = cYAML_get_object_item(tree, "tunables");
2472 item = cYAML_get_object_item(tun, "peer_timeout");
2474 tunables->lct_peer_timeout = item->cy_valueint;
2475 item = cYAML_get_object_item(tun, "peer_credits");
2477 tunables->lct_peer_tx_credits = item->cy_valueint;
2478 item = cYAML_get_object_item(tun, "peer_buffer_credits");
2480 tunables->lct_peer_rtr_credits = item->cy_valueint;
2481 item = cYAML_get_object_item(tun, "credits");
2483 tunables->lct_max_tx_credits = item->cy_valueint;
2484 smp = cYAML_get_object_item(tun, "CPT");
2486 rc = cfs_expr_list_parse(smp->cy_valuestring,
2487 strlen(smp->cy_valuestring),
2488 0, UINT_MAX, global_cpts);
2490 *global_cpts = NULL;
2500 yaml_extract_tunables(struct cYAML *tree,
2501 struct lnet_ioctl_config_lnd_tunables *tunables,
2502 struct cfs_expr_list **global_cpts,
2507 rc = yaml_extract_cmn_tunables(tree, &tunables->lt_cmn,
2513 lustre_yaml_extract_lnd_tunables(tree, net_type,
2521 * - net type: <net>[<NUM>]
2523 * - nid: <ip>@<net>[<NUM>]
2526 * 0: <intf_name>['['<expr>']']
2527 * 1: <intf_name>['['<expr>']']
2529 * peer_timeout: <NUM>
2530 * peer_credits: <NUM>
2531 * peer_buffer_credits: <NUM>
2534 * peercredits_hiw: <NUM>
2535 * map_on_demand: <NUM>
2536 * concurrent_sends: <NUM>
2537 * fmr_pool_size: <NUM>
2538 * fmr_flush_trigger: <NUM>
2541 * At least one interface is required. If no interfaces are provided the
2542 * network interface can not be configured.
2544 static int handle_yaml_config_ni(struct cYAML *tree, struct cYAML **show_rc,
2545 struct cYAML **err_rc)
2547 struct cYAML *net, *intf, *seq_no, *ip2net = NULL, *local_nis = NULL,
2549 int num_entries = 0, rc;
2550 struct lnet_dlc_network_descr nw_descr;
2551 struct cfs_expr_list *global_cpts = NULL;
2552 struct lnet_ioctl_config_lnd_tunables tunables;
2555 memset(&tunables, 0, sizeof(tunables));
2557 INIT_LIST_HEAD(&nw_descr.network_on_rule);
2558 INIT_LIST_HEAD(&nw_descr.nw_intflist);
2560 ip2net = cYAML_get_object_item(tree, "ip2net");
2561 net = cYAML_get_object_item(tree, "net type");
2563 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
2565 nw_descr.nw_id = LOLND;
2568 * if neither net nor ip2nets are present, then we can not
2569 * configure the network.
2571 if (!net && !ip2net)
2572 return LUSTRE_CFG_RC_MISSING_PARAM;
2574 local_nis = cYAML_get_object_item(tree, "local NI(s)");
2575 if (local_nis == NULL)
2576 return LUSTRE_CFG_RC_MISSING_PARAM;
2578 if (!cYAML_is_sequence(local_nis))
2579 return LUSTRE_CFG_RC_BAD_PARAM;
2581 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
2582 intf = cYAML_get_object_item(item, "interfaces");
2585 num_entries = yaml_copy_intf_info(intf, &nw_descr);
2586 if (num_entries <= 0) {
2587 cYAML_build_error(num_entries, -1, "ni", "add",
2588 "bad interface list",
2590 return LUSTRE_CFG_RC_BAD_PARAM;
2594 found = yaml_extract_tunables(tree, &tunables, &global_cpts,
2595 LNET_NETTYP(nw_descr.nw_id));
2596 seq_no = cYAML_get_object_item(tree, "seq_no");
2598 rc = lustre_lnet_config_ni(&nw_descr,
2600 (ip2net) ? ip2net->cy_valuestring : NULL,
2601 (found) ? &tunables: NULL,
2602 (seq_no) ? seq_no->cy_valueint : -1,
2605 if (global_cpts != NULL)
2606 cfs_expr_list_free(global_cpts);
2613 * - net-spec: <tcp|o2ib|gni>[NUM]
2615 * 0: <intf name>['['<expr>']']
2616 * 1: <intf name>['['<expr>']']
2618 * 0: <expr.expr.expr.expr>
2619 * 1: <expr.expr.expr.expr>
2621 static int handle_yaml_config_ip2nets(struct cYAML *tree,
2622 struct cYAML **show_rc,
2623 struct cYAML **err_rc)
2625 struct cYAML *net, *ip_range, *item = NULL, *intf = NULL,
2627 struct lustre_lnet_ip2nets ip2nets;
2628 struct lustre_lnet_ip_range_descr *ip_range_descr = NULL,
2630 int rc = LUSTRE_CFG_RC_NO_ERR;
2631 struct cfs_expr_list *global_cpts = NULL;
2632 struct cfs_expr_list *el, *el_tmp;
2633 struct lnet_ioctl_config_lnd_tunables tunables;
2634 struct lnet_dlc_intf_descr *intf_descr, *intf_tmp;
2637 memset(&tunables, 0, sizeof(tunables));
2639 /* initialize all lists */
2640 INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
2641 INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
2642 INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
2644 net = cYAML_get_object_item(tree, "net-spec");
2646 return LUSTRE_CFG_RC_BAD_PARAM;
2648 if (net != NULL && net->cy_valuestring == NULL)
2649 return LUSTRE_CFG_RC_BAD_PARAM;
2651 /* assign the network id */
2652 ip2nets.ip2nets_net.nw_id = libcfs_str2net(net->cy_valuestring);
2653 if (ip2nets.ip2nets_net.nw_id == LNET_NID_ANY)
2654 return LUSTRE_CFG_RC_BAD_PARAM;
2656 seq_no = cYAML_get_object_item(tree, "seq_no");
2658 intf = cYAML_get_object_item(tree, "interfaces");
2660 rc = yaml_copy_intf_info(intf, &ip2nets.ip2nets_net);
2662 return LUSTRE_CFG_RC_BAD_PARAM;
2665 ip_range = cYAML_get_object_item(tree, "ip-range");
2666 if (ip_range != NULL) {
2667 item = ip_range->cy_child;
2668 while (item != NULL) {
2669 if (item->cy_valuestring == NULL) {
2670 item = item->cy_next;
2674 rc = lustre_lnet_add_ip_range(&ip2nets.ip2nets_ip_ranges,
2675 item->cy_valuestring);
2677 if (rc != LUSTRE_CFG_RC_NO_ERR)
2680 item = item->cy_next;
2684 found = yaml_extract_tunables(tree, &tunables, &global_cpts,
2685 LNET_NETTYP(ip2nets.ip2nets_net.nw_id));
2687 rc = lustre_lnet_config_ip2nets(&ip2nets,
2688 (found) ? &tunables : NULL,
2690 (seq_no) ? seq_no->cy_valueint : -1,
2694 * don't stop because there was no match. Continue processing the
2695 * rest of the rules. If non-match then nothing is configured
2697 if (rc == LUSTRE_CFG_RC_NO_MATCH)
2698 rc = LUSTRE_CFG_RC_NO_ERR;
2700 list_for_each_entry_safe(intf_descr, intf_tmp,
2701 &ip2nets.ip2nets_net.nw_intflist,
2703 list_del(&intf_descr->intf_on_network);
2704 free_intf_descr(intf_descr);
2707 list_for_each_entry_safe(ip_range_descr, tmp,
2708 &ip2nets.ip2nets_ip_ranges,
2710 list_del(&ip_range_descr->ipr_entry);
2711 list_for_each_entry_safe(el, el_tmp, &ip_range_descr->ipr_expr,
2713 list_del(&el->el_link);
2714 cfs_expr_list_free(el);
2716 free(ip_range_descr);
2722 static int handle_yaml_del_ni(struct cYAML *tree, struct cYAML **show_rc,
2723 struct cYAML **err_rc)
2725 struct cYAML *net = NULL, *intf = NULL, *seq_no = NULL, *item = NULL,
2727 int num_entries, rc;
2728 struct lnet_dlc_network_descr nw_descr;
2730 INIT_LIST_HEAD(&nw_descr.network_on_rule);
2731 INIT_LIST_HEAD(&nw_descr.nw_intflist);
2733 net = cYAML_get_object_item(tree, "net type");
2735 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
2737 local_nis = cYAML_get_object_item(tree, "local NI(s)");
2738 if (local_nis == NULL)
2739 return LUSTRE_CFG_RC_MISSING_PARAM;
2741 if (!cYAML_is_sequence(local_nis))
2742 return LUSTRE_CFG_RC_BAD_PARAM;
2744 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
2745 intf = cYAML_get_object_item(item, "interfaces");
2748 num_entries = yaml_copy_intf_info(intf, &nw_descr);
2749 if (num_entries <= 0) {
2750 cYAML_build_error(num_entries, -1, "ni", "add",
2751 "bad interface list",
2753 return LUSTRE_CFG_RC_BAD_PARAM;
2757 seq_no = cYAML_get_object_item(tree, "seq_no");
2759 rc = lustre_lnet_del_ni((net) ? &nw_descr : NULL,
2760 (seq_no) ? seq_no->cy_valueint : -1,
2766 static int yaml_copy_peer_nids(struct cYAML *tree, char ***nidsppp, bool del)
2768 struct cYAML *nids_entry = NULL, *child = NULL, *entry = NULL,
2771 int num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
2773 prim_nid = cYAML_get_object_item(tree, "primary nid");
2774 if (!prim_nid || !prim_nid->cy_valuestring)
2775 return LUSTRE_CFG_RC_MISSING_PARAM;
2777 nids_entry = cYAML_get_object_item(tree, "peer ni");
2778 if (cYAML_is_sequence(nids_entry)) {
2779 while (cYAML_get_next_seq_item(nids_entry, &child)) {
2780 entry = cYAML_get_object_item(child, "nid");
2781 /* don't count an empty entry */
2782 if (!entry || !entry->cy_valuestring)
2785 if ((strcmp(entry->cy_valuestring, prim_nid->cy_valuestring)
2788 * primary nid is present in the list of
2789 * nids so that means we want to delete
2790 * the entire peer, so no need to go
2791 * further. Just delete the entire peer.
2801 return LUSTRE_CFG_RC_MISSING_PARAM;
2803 nids = calloc(sizeof(*nids) * num, 1);
2805 return LUSTRE_CFG_RC_OUT_OF_MEM;
2807 /* now grab all the nids */
2810 while (cYAML_get_next_seq_item(nids_entry, &child)) {
2811 entry = cYAML_get_object_item(child, "nid");
2812 if (!entry || !entry->cy_valuestring)
2815 nids[num] = calloc(strlen(entry->cy_valuestring) + 1, 1);
2817 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2820 strncpy(nids[num], entry->cy_valuestring,
2821 strlen(entry->cy_valuestring));
2831 yaml_free_string_array(nids, num);
2836 static int handle_yaml_config_peer(struct cYAML *tree, struct cYAML **show_rc,
2837 struct cYAML **err_rc)
2841 struct cYAML *seq_no, *prim_nid, *non_mr;
2843 num = yaml_copy_peer_nids(tree, &nids, false);
2847 seq_no = cYAML_get_object_item(tree, "seq_no");
2848 prim_nid = cYAML_get_object_item(tree, "primary nid");
2849 non_mr = cYAML_get_object_item(tree, "non_mr");
2851 rc = lustre_lnet_config_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
2853 (non_mr) ? false : true,
2854 (seq_no) ? seq_no->cy_valueint : -1,
2857 yaml_free_string_array(nids, num);
2861 static int handle_yaml_del_peer(struct cYAML *tree, struct cYAML **show_rc,
2862 struct cYAML **err_rc)
2866 struct cYAML *seq_no, *prim_nid;
2868 num = yaml_copy_peer_nids(tree, &nids, true);
2872 seq_no = cYAML_get_object_item(tree, "seq_no");
2873 prim_nid = cYAML_get_object_item(tree, "primary nid");
2875 rc = lustre_lnet_del_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
2877 (seq_no) ? seq_no->cy_valueint : -1,
2880 yaml_free_string_array(nids, num);
2884 static int handle_yaml_config_buffers(struct cYAML *tree,
2885 struct cYAML **show_rc,
2886 struct cYAML **err_rc)
2889 struct cYAML *tiny, *small, *large, *seq_no;
2891 tiny = cYAML_get_object_item(tree, "tiny");
2892 small = cYAML_get_object_item(tree, "small");
2893 large = cYAML_get_object_item(tree, "large");
2894 seq_no = cYAML_get_object_item(tree, "seq_no");
2896 rc = lustre_lnet_config_buffers((tiny) ? tiny->cy_valueint : -1,
2897 (small) ? small->cy_valueint : -1,
2898 (large) ? large->cy_valueint : -1,
2899 (seq_no) ? seq_no->cy_valueint : -1,
2905 static int handle_yaml_config_routing(struct cYAML *tree,
2906 struct cYAML **show_rc,
2907 struct cYAML **err_rc)
2909 int rc = LUSTRE_CFG_RC_NO_ERR;
2910 struct cYAML *seq_no, *enable;
2912 seq_no = cYAML_get_object_item(tree, "seq_no");
2913 enable = cYAML_get_object_item(tree, "enable");
2916 rc = lustre_lnet_enable_routing(enable->cy_valueint,
2918 seq_no->cy_valueint : -1,
2925 static int handle_yaml_del_route(struct cYAML *tree, struct cYAML **show_rc,
2926 struct cYAML **err_rc)
2930 struct cYAML *seq_no;
2932 net = cYAML_get_object_item(tree, "net");
2933 gw = cYAML_get_object_item(tree, "gateway");
2934 seq_no = cYAML_get_object_item(tree, "seq_no");
2936 return lustre_lnet_del_route((net) ? net->cy_valuestring : NULL,
2937 (gw) ? gw->cy_valuestring : NULL,
2938 (seq_no) ? seq_no->cy_valueint : -1,
2942 static int handle_yaml_del_routing(struct cYAML *tree, struct cYAML **show_rc,
2943 struct cYAML **err_rc)
2945 struct cYAML *seq_no;
2947 seq_no = cYAML_get_object_item(tree, "seq_no");
2949 return lustre_lnet_enable_routing(0, (seq_no) ?
2950 seq_no->cy_valueint : -1,
2954 static int handle_yaml_show_route(struct cYAML *tree, struct cYAML **show_rc,
2955 struct cYAML **err_rc)
2961 struct cYAML *detail;
2962 struct cYAML *seq_no;
2964 net = cYAML_get_object_item(tree, "net");
2965 gw = cYAML_get_object_item(tree, "gateway");
2966 hop = cYAML_get_object_item(tree, "hop");
2967 prio = cYAML_get_object_item(tree, "priority");
2968 detail = cYAML_get_object_item(tree, "detail");
2969 seq_no = cYAML_get_object_item(tree, "seq_no");
2971 return lustre_lnet_show_route((net) ? net->cy_valuestring : NULL,
2972 (gw) ? gw->cy_valuestring : NULL,
2973 (hop) ? hop->cy_valueint : -1,
2974 (prio) ? prio->cy_valueint : -1,
2975 (detail) ? detail->cy_valueint : 0,
2976 (seq_no) ? seq_no->cy_valueint : -1,
2981 static int handle_yaml_show_net(struct cYAML *tree, struct cYAML **show_rc,
2982 struct cYAML **err_rc)
2984 struct cYAML *net, *detail, *seq_no;
2986 net = cYAML_get_object_item(tree, "net");
2987 detail = cYAML_get_object_item(tree, "detail");
2988 seq_no = cYAML_get_object_item(tree, "seq_no");
2990 return lustre_lnet_show_net((net) ? net->cy_valuestring : NULL,
2991 (detail) ? detail->cy_valueint : 0,
2992 (seq_no) ? seq_no->cy_valueint : -1,
2997 static int handle_yaml_show_routing(struct cYAML *tree, struct cYAML **show_rc,
2998 struct cYAML **err_rc)
3000 struct cYAML *seq_no;
3002 seq_no = cYAML_get_object_item(tree, "seq_no");
3004 return lustre_lnet_show_routing((seq_no) ? seq_no->cy_valueint : -1,
3008 static int handle_yaml_show_peers(struct cYAML *tree, struct cYAML **show_rc,
3009 struct cYAML **err_rc)
3011 struct cYAML *seq_no, *nid, *detail;
3013 seq_no = cYAML_get_object_item(tree, "seq_no");
3014 detail = cYAML_get_object_item(tree, "detail");
3015 nid = cYAML_get_object_item(tree, "nid");
3017 return lustre_lnet_show_peer((nid) ? nid->cy_valuestring : NULL,
3018 (detail) ? detail->cy_valueint : 0,
3019 (seq_no) ? seq_no->cy_valueint : -1,
3023 static int handle_yaml_show_stats(struct cYAML *tree, struct cYAML **show_rc,
3024 struct cYAML **err_rc)
3026 struct cYAML *seq_no;
3028 seq_no = cYAML_get_object_item(tree, "seq_no");
3030 return lustre_lnet_show_stats((seq_no) ? seq_no->cy_valueint : -1,
3034 static int handle_yaml_config_numa(struct cYAML *tree, struct cYAML **show_rc,
3035 struct cYAML **err_rc)
3037 struct cYAML *seq_no, *range;
3039 seq_no = cYAML_get_object_item(tree, "seq_no");
3040 range = cYAML_get_object_item(tree, "range");
3042 return lustre_lnet_config_numa_range(range ? range->cy_valueint : -1,
3043 seq_no ? seq_no->cy_valueint : -1,
3047 static int handle_yaml_del_numa(struct cYAML *tree, struct cYAML **show_rc,
3048 struct cYAML **err_rc)
3050 struct cYAML *seq_no;
3052 seq_no = cYAML_get_object_item(tree, "seq_no");
3054 return lustre_lnet_config_numa_range(0, seq_no ? seq_no->cy_valueint : -1,
3058 static int handle_yaml_show_numa(struct cYAML *tree, struct cYAML **show_rc,
3059 struct cYAML **err_rc)
3061 struct cYAML *seq_no;
3063 seq_no = cYAML_get_object_item(tree, "seq_no");
3065 return lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint : -1,
3069 struct lookup_cmd_hdlr_tbl {
3074 static struct lookup_cmd_hdlr_tbl lookup_config_tbl[] = {
3075 { .name = "route", .cb = handle_yaml_config_route },
3076 { .name = "net", .cb = handle_yaml_config_ni },
3077 { .name = "ip2nets", .cb = handle_yaml_config_ip2nets },
3078 { .name = "peer", .cb = handle_yaml_config_peer },
3079 { .name = "routing", .cb = handle_yaml_config_routing },
3080 { .name = "buffers", .cb = handle_yaml_config_buffers },
3081 { .name = "numa", .cb = handle_yaml_config_numa },
3084 static struct lookup_cmd_hdlr_tbl lookup_del_tbl[] = {
3085 { .name = "route", .cb = handle_yaml_del_route },
3086 { .name = "net", .cb = handle_yaml_del_ni },
3087 { .name = "peer", .cb = handle_yaml_del_peer },
3088 { .name = "routing", .cb = handle_yaml_del_routing },
3089 { .name = "numa", .cb = handle_yaml_del_numa },
3092 static struct lookup_cmd_hdlr_tbl lookup_show_tbl[] = {
3093 { .name = "route", .cb = handle_yaml_show_route },
3094 { .name = "net", .cb = handle_yaml_show_net },
3095 { .name = "buffers", .cb = handle_yaml_show_routing },
3096 { .name = "routing", .cb = handle_yaml_show_routing },
3097 { .name = "peer", .cb = handle_yaml_show_peers },
3098 { .name = "statistics", .cb = handle_yaml_show_stats },
3099 { .name = "numa", .cb = handle_yaml_show_numa },
3102 static cmd_handler_t lookup_fn(char *key,
3103 struct lookup_cmd_hdlr_tbl *tbl)
3109 for (i = 0; tbl[i].name != NULL; i++) {
3110 if (strncmp(key, tbl[i].name, strlen(tbl[i].name)) == 0)
3117 static int lustre_yaml_cb_helper(char *f, struct lookup_cmd_hdlr_tbl *table,
3118 struct cYAML **show_rc, struct cYAML **err_rc)
3120 struct cYAML *tree, *item = NULL, *head, *child;
3122 char err_str[LNET_MAX_STR_LEN];
3123 int rc = LUSTRE_CFG_RC_NO_ERR, return_rc = LUSTRE_CFG_RC_NO_ERR;
3125 tree = cYAML_build_tree(f, NULL, 0, err_rc, false);
3127 return LUSTRE_CFG_RC_BAD_PARAM;
3129 child = tree->cy_child;
3130 while (child != NULL) {
3131 cb = lookup_fn(child->cy_string, table);
3133 snprintf(err_str, sizeof(err_str),
3134 "\"call back for '%s' not found\"",
3136 cYAML_build_error(LUSTRE_CFG_RC_BAD_PARAM, -1,
3137 "yaml", "helper", err_str, err_rc);
3141 if (cYAML_is_sequence(child)) {
3142 while ((head = cYAML_get_next_seq_item(child, &item))
3144 rc = cb(head, show_rc, err_rc);
3145 if (rc != LUSTRE_CFG_RC_NO_ERR)
3149 rc = cb(child, show_rc, err_rc);
3150 if (rc != LUSTRE_CFG_RC_NO_ERR)
3154 child = child->cy_next;
3158 cYAML_free_tree(tree);
3163 int lustre_yaml_config(char *f, struct cYAML **err_rc)
3165 return lustre_yaml_cb_helper(f, lookup_config_tbl,
3169 int lustre_yaml_del(char *f, struct cYAML **err_rc)
3171 return lustre_yaml_cb_helper(f, lookup_del_tbl,
3175 int lustre_yaml_show(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
3177 return lustre_yaml_cb_helper(f, lookup_show_tbl,