4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of the
9 * License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 * Copyright (c) 2014, 2016, Intel Corporation.
24 * Amir Shehata <amir.shehata@intel.com>
29 * 1. APIs that take the actual parameters expanded. This is for other
30 * entities that would like to link against the library and call the APIs
31 * directly without having to form an intermediate representation.
32 * 2. APIs that take a YAML file and parses out the information there and
33 * calls the APIs mentioned in 1
43 #include <sys/ioctl.h>
45 #include <libcfs/util/ioctl.h>
46 #include <lnet/lnetctl.h>
47 #include <lnet/socklnd.h>
49 #include <lnet/lnet.h>
50 #include <libcfs/libcfs_string.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;
83 * lustre_lnet_add_ip_range
85 * given a string of the format:
86 * <expr.expr.expr.expr> parse each expr into
87 * a lustre_lnet_ip_range_descr structure and insert on the list.
89 * This function is called from
90 * YAML on each ip-range.
91 * As a result of lnetctl command
92 * When building a NID or P2P selection rules
94 int lustre_lnet_add_ip_range(struct list_head *list, char *str_ip_range)
96 struct lustre_lnet_ip_range_descr *ip_range;
99 ip_range = calloc(1, sizeof(*ip_range));
100 if (ip_range == NULL)
101 return LUSTRE_CFG_RC_OUT_OF_MEM;
103 INIT_LIST_HEAD(&ip_range->ipr_entry);
104 INIT_LIST_HEAD(&ip_range->ipr_expr);
106 rc = cfs_ip_addr_parse(str_ip_range, strlen(str_ip_range),
107 &ip_range->ipr_expr);
109 return LUSTRE_CFG_RC_BAD_PARAM;
111 list_add_tail(&ip_range->ipr_entry, list);
113 return LUSTRE_CFG_RC_NO_ERR;
116 int lustre_lnet_add_intf_descr(struct list_head *list, char *intf, int len)
118 char *open_sq_bracket = NULL, *close_sq_bracket = NULL,
120 struct lnet_dlc_intf_descr *intf_descr = NULL;
122 char intf_string[LNET_MAX_STR_LEN];
124 if (len >= LNET_MAX_STR_LEN)
125 return LUSTRE_CFG_RC_BAD_PARAM;
127 strncpy(intf_string, intf, len);
128 intf_string[len] = '\0';
130 intf_descr = calloc(1, sizeof(*intf_descr));
131 if (intf_descr == NULL)
132 return LUSTRE_CFG_RC_OUT_OF_MEM;
134 INIT_LIST_HEAD(&intf_descr->intf_on_network);
136 intf_name = intf_string;
137 open_sq_bracket = strchr(intf_string, '[');
138 if (open_sq_bracket != NULL) {
139 close_sq_bracket = strchr(intf_string, ']');
140 if (close_sq_bracket == NULL) {
142 return LUSTRE_CFG_RC_BAD_PARAM;
144 rc = cfs_expr_list_parse(open_sq_bracket,
145 strlen(open_sq_bracket), 0, UINT_MAX,
146 &intf_descr->cpt_expr);
149 return LUSTRE_CFG_RC_BAD_PARAM;
151 strncpy(intf_descr->intf_name, intf_name,
152 open_sq_bracket - intf_name);
153 intf_descr->intf_name[open_sq_bracket - intf_name] = '\0';
155 strcpy(intf_descr->intf_name, intf_name);
156 intf_descr->cpt_expr = NULL;
159 list_add_tail(&intf_descr->intf_on_network, list);
161 return LUSTRE_CFG_RC_NO_ERR;
164 void lustre_lnet_init_nw_descr(struct lnet_dlc_network_descr *nw_descr)
166 if (nw_descr != NULL) {
167 INIT_LIST_HEAD(&nw_descr->network_on_rule);
168 INIT_LIST_HEAD(&nw_descr->nw_intflist);
172 int lustre_lnet_parse_nids(char *nids, char **array, int size,
176 char *comma = nids, *cur, *entry;
178 int i, len, start = 0, finish = 0;
180 if (nids == NULL || strlen(nids) == 0)
183 /* count the number or new nids, by counting the number of commas */
185 comma = strchr(comma, ',');
195 * if the array is not NULL allocate a large enough array to house
196 * the old and new entries
198 new_array = calloc(sizeof(char*),
199 (size > 0) ? size + num_nids : num_nids);
204 /* parse our the new nids and add them to the tail of the array */
207 start = (size > 0) ? size: 0;
208 finish = (size > 0) ? size + num_nids : num_nids;
209 for (i = start; i < finish; i++) {
210 comma = strchr(comma, ',');
213 * the length of the string to be parsed out is
214 * from cur to end of string. So it's good enough
217 len = strlen(cur) + 1;
219 /* length of the string is comma - cur */
220 len = (comma - cur) + 1;
222 entry = calloc(1, len);
224 finish = i > 0 ? i - 1: 0;
227 strncpy(entry, cur, len - 1);
229 new_array[i] = entry;
236 /* add the old entries in the array and delete the old array*/
237 for (i = 0; i < size; i++)
238 new_array[i] = array[i];
243 *out_array = new_array;
248 for (i = start; i < finish; i++)
258 * <intf>[<expr>], <intf>[<expr>],..
260 int lustre_lnet_parse_interfaces(char *intf_str,
261 struct lnet_dlc_network_descr *nw_descr)
266 char *cur = intf_str, *next = NULL;
267 char *end = intf_str + strlen(intf_str);
269 struct lnet_dlc_intf_descr *intf_descr, *tmp;
271 if (nw_descr == NULL)
272 return LUSTRE_CFG_RC_BAD_PARAM;
275 open_square = strchr(cur, '[');
276 if (open_square != NULL) {
277 close_square = strchr(cur, ']');
278 if (close_square == NULL) {
279 rc = LUSTRE_CFG_RC_BAD_PARAM;
283 comma = strchr(cur, ',');
284 if (comma != NULL && comma > close_square) {
286 len = next - close_square;
292 comma = strchr(cur, ',');
302 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist, cur, len);
303 if (rc != LUSTRE_CFG_RC_NO_ERR)
309 return LUSTRE_CFG_RC_NO_ERR;
312 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
314 if (intf_descr->cpt_expr != NULL)
315 cfs_expr_list_free(intf_descr->cpt_expr);
322 int lustre_lnet_config_lib_init(void)
324 return register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH,
325 LNET_DEV_MAJOR, LNET_DEV_MINOR);
328 void lustre_lnet_config_lib_uninit(void)
330 unregister_ioc_dev(LNET_DEV_ID);
333 int lustre_lnet_config_ni_system(bool up, bool load_ni_from_mod,
334 int seq_no, struct cYAML **err_rc)
336 struct libcfs_ioctl_data data;
339 char err_str[LNET_MAX_STR_LEN];
341 snprintf(err_str, sizeof(err_str), "\"Success\"");
343 LIBCFS_IOC_INIT(data);
345 /* Reverse logic is used here in order not to change
346 * the lctl utility */
347 data.ioc_flags = load_ni_from_mod ? 0 : 1;
349 opc = up ? IOC_LIBCFS_CONFIGURE : IOC_LIBCFS_UNCONFIGURE;
351 rc = l_ioctl(LNET_DEV_ID, opc, &data);
355 "\"LNet %s error: %s\"", (up) ? "configure" :
356 "unconfigure", strerror(errno));
360 cYAML_build_error(rc, seq_no, (up) ? CONFIG_CMD : UNCONFIG_CMD,
361 "lnet", err_str, err_rc);
366 static lnet_nid_t *allocate_create_nid_array(char **nids, __u32 num_nids,
369 lnet_nid_t *array = NULL;
373 snprintf(err_str, LNET_MAX_STR_LEN, "no NIDs to add");
377 array = calloc(sizeof(*array) * num_nids, 1);
379 snprintf(err_str, LNET_MAX_STR_LEN, "out of memory");
383 for (i = 0; i < num_nids; i++) {
384 array[i] = libcfs_str2nid(nids[i]);
385 if (array[i] == LNET_NID_ANY) {
387 snprintf(err_str, LNET_MAX_STR_LEN,
397 static int dispatch_peer_ni_cmd(lnet_nid_t knid, lnet_nid_t nid, __u32 cmd,
398 struct lnet_ioctl_peer_cfg *data,
399 char *err_str, char *cmd_str)
403 data->prcfg_key_nid = knid;
404 data->prcfg_cfg_nid = nid;
406 rc = l_ioctl(LNET_DEV_ID, cmd, data);
411 "\"cannot %s peer ni: %s\"",
412 (cmd_str) ? cmd_str : "add", strerror(errno));
418 int lustre_lnet_config_peer_nid(char *knid, char **nid, int num_nids,
419 bool mr, int seq_no, struct cYAML **err_rc)
421 struct lnet_ioctl_peer_cfg data;
422 lnet_nid_t key_nid = LNET_NID_ANY;
423 int rc = LUSTRE_CFG_RC_NO_ERR;
425 bool nid0_used = false;
426 char err_str[LNET_MAX_STR_LEN] = {0};
427 lnet_nid_t *nids = allocate_create_nid_array(nid, num_nids, err_str);
430 key_nid = libcfs_str2nid(knid);
431 if (key_nid == LNET_NID_ANY) {
432 snprintf(err_str, sizeof(err_str),
435 rc = LUSTRE_CFG_RC_MISSING_PARAM;
438 } else if (!nids || nids[0] == LNET_NID_ANY) {
439 snprintf(err_str, sizeof(err_str),
440 "no NIDs provided for configuration");
441 rc = LUSTRE_CFG_RC_MISSING_PARAM;
444 key_nid = LNET_NID_ANY;
447 snprintf(err_str, sizeof(err_str), "\"Success\"");
449 LIBCFS_IOC_INIT_V2(data, prcfg_hdr);
453 * if key_nid is not specified use the first nid in the list of
454 * nids provided as the key_nid. NOTE: on entering 'if' we must
455 * have at least 1 NID
457 if (key_nid == LNET_NID_ANY) {
462 /* Create the key_nid first */
463 rc = dispatch_peer_ni_cmd(key_nid, LNET_NID_ANY,
464 IOC_LIBCFS_ADD_PEER_NI,
465 &data, err_str, "add");
470 /* add the rest of the nids to the key nid if any are available */
471 for (idx = nid0_used ? 1 : 0 ; nids && idx < num_nids; idx++) {
473 * If key_nid is not provided then the first nid in the
474 * list becomes the key_nid. First time round the loop use
475 * LNET_NID_ANY for the first parameter, then use nid[0]
476 * as the key nid after wards
478 rc = dispatch_peer_ni_cmd(key_nid, nids[idx],
479 IOC_LIBCFS_ADD_PEER_NI, &data,
489 cYAML_build_error(rc, seq_no, ADD_CMD, "peer_ni", err_str, err_rc);
493 int lustre_lnet_del_peer_nid(char *knid, char **nid, int num_nids,
494 int seq_no, struct cYAML **err_rc)
496 struct lnet_ioctl_peer_cfg data;
498 int rc = LUSTRE_CFG_RC_NO_ERR;
500 char err_str[LNET_MAX_STR_LEN] = {0};
501 lnet_nid_t *nids = allocate_create_nid_array(nid, num_nids, err_str);
504 snprintf(err_str, sizeof(err_str),
505 "\"Primary nid is not provided\"");
506 rc = LUSTRE_CFG_RC_MISSING_PARAM;
509 key_nid = libcfs_str2nid(knid);
510 if (key_nid == LNET_NID_ANY) {
511 rc = LUSTRE_CFG_RC_BAD_PARAM;
512 snprintf(err_str, sizeof(err_str),
519 snprintf(err_str, sizeof(err_str), "\"Success\"");
521 LIBCFS_IOC_INIT_V2(data, prcfg_hdr);
522 if (!nids || nids[0] == LNET_NID_ANY) {
523 rc = dispatch_peer_ni_cmd(key_nid, LNET_NID_ANY,
524 IOC_LIBCFS_DEL_PEER_NI,
525 &data, err_str, "del");
529 for (idx = 0; nids && idx < num_nids; idx++) {
530 rc = dispatch_peer_ni_cmd(key_nid, nids[idx],
531 IOC_LIBCFS_DEL_PEER_NI, &data,
541 cYAML_build_error(rc, seq_no, DEL_CMD, "peer_ni", err_str, err_rc);
545 int lustre_lnet_config_route(char *nw, char *gw, int hops, int prio,
546 int seq_no, struct cYAML **err_rc)
548 struct lnet_ioctl_config_data data;
549 lnet_nid_t gateway_nid;
550 int rc = LUSTRE_CFG_RC_NO_ERR;
551 __u32 net = LNET_NIDNET(LNET_NID_ANY);
552 char err_str[LNET_MAX_STR_LEN];
554 snprintf(err_str, sizeof(err_str), "\"Success\"");
556 if (nw == NULL || gw == NULL) {
559 "\"missing mandatory parameter(s): '%s'\"",
560 (nw == NULL && gw == NULL) ? "network, gateway" :
561 (nw == NULL) ? "network" : "gateway");
562 rc = LUSTRE_CFG_RC_MISSING_PARAM;
566 net = libcfs_str2net(nw);
567 if (net == LNET_NIDNET(LNET_NID_ANY)) {
570 "\"cannot parse net %s\"", nw);
571 rc = LUSTRE_CFG_RC_BAD_PARAM;
575 if (LNET_NETTYP(net) == CIBLND ||
576 LNET_NETTYP(net) == OPENIBLND ||
577 LNET_NETTYP(net) == IIBLND ||
578 LNET_NETTYP(net) == VIBLND) {
581 "\"obselete LNet type '%s'\"", libcfs_lnd2str(net));
582 rc = LUSTRE_CFG_RC_BAD_PARAM;
586 gateway_nid = libcfs_str2nid(gw);
587 if (gateway_nid == LNET_NID_ANY) {
590 "\"cannot parse gateway NID '%s'\"", gw);
591 rc = LUSTRE_CFG_RC_BAD_PARAM;
596 /* hops is undefined */
597 hops = LNET_UNDEFINED_HOPS;
598 } else if (hops < 1 || hops > 255) {
601 "\"invalid hop count %d, must be between 1 and 255\"",
603 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
609 } else if (prio < 0) {
612 "\"invalid priority %d, must be greater than 0\"",
614 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
618 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
620 data.cfg_config_u.cfg_route.rtr_hop = hops;
621 data.cfg_config_u.cfg_route.rtr_priority = prio;
622 data.cfg_nid = gateway_nid;
624 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
629 "\"cannot add route: %s\"", strerror(errno));
634 cYAML_build_error(rc, seq_no, ADD_CMD, "route", err_str, err_rc);
639 int lustre_lnet_del_route(char *nw, char *gw,
640 int seq_no, struct cYAML **err_rc)
642 struct lnet_ioctl_config_data data;
643 lnet_nid_t gateway_nid;
644 int rc = LUSTRE_CFG_RC_NO_ERR;
645 __u32 net = LNET_NIDNET(LNET_NID_ANY);
646 char err_str[LNET_MAX_STR_LEN];
648 snprintf(err_str, sizeof(err_str), "\"Success\"");
650 if (nw == NULL || gw == NULL) {
653 "\"missing mandatory parameter(s): '%s'\"",
654 (nw == NULL && gw == NULL) ? "network, gateway" :
655 (nw == NULL) ? "network" : "gateway");
656 rc = LUSTRE_CFG_RC_MISSING_PARAM;
660 net = libcfs_str2net(nw);
661 if (net == LNET_NIDNET(LNET_NID_ANY)) {
664 "\"cannot parse net '%s'\"", nw);
665 rc = LUSTRE_CFG_RC_BAD_PARAM;
669 if (LNET_NETTYP(net) == CIBLND ||
670 LNET_NETTYP(net) == OPENIBLND ||
671 LNET_NETTYP(net) == IIBLND ||
672 LNET_NETTYP(net) == VIBLND) {
675 "\"obselete LNet type '%s'\"", libcfs_lnd2str(net));
676 rc = LUSTRE_CFG_RC_BAD_PARAM;
680 gateway_nid = libcfs_str2nid(gw);
681 if (gateway_nid == LNET_NID_ANY) {
684 "\"cannot parse gateway NID '%s'\"", gw);
685 rc = LUSTRE_CFG_RC_BAD_PARAM;
689 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
691 data.cfg_nid = gateway_nid;
693 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
698 "\"cannot delete route: %s\"", strerror(errno));
703 cYAML_build_error(rc, seq_no, DEL_CMD, "route", err_str, err_rc);
708 int lustre_lnet_show_route(char *nw, char *gw, int hops, int prio, int detail,
709 int seq_no, struct cYAML **show_rc,
710 struct cYAML **err_rc)
712 struct lnet_ioctl_config_data data;
713 lnet_nid_t gateway_nid;
714 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
716 __u32 net = LNET_NIDNET(LNET_NID_ANY);
718 struct cYAML *root = NULL, *route = NULL, *item = NULL;
719 struct cYAML *first_seq = NULL;
720 char err_str[LNET_MAX_STR_LEN];
723 snprintf(err_str, sizeof(err_str),
724 "\"out of memory\"");
727 net = libcfs_str2net(nw);
728 if (net == LNET_NIDNET(LNET_NID_ANY)) {
731 "\"cannot parse net '%s'\"", nw);
732 rc = LUSTRE_CFG_RC_BAD_PARAM;
736 if (LNET_NETTYP(net) == CIBLND ||
737 LNET_NETTYP(net) == OPENIBLND ||
738 LNET_NETTYP(net) == IIBLND ||
739 LNET_NETTYP(net) == VIBLND) {
742 "\"obsolete LNet type '%s'\"",
743 libcfs_lnd2str(net));
744 rc = LUSTRE_CFG_RC_BAD_PARAM;
748 /* show all routes without filtering on net */
749 net = LNET_NIDNET(LNET_NID_ANY);
753 gateway_nid = libcfs_str2nid(gw);
754 if (gateway_nid == LNET_NID_ANY) {
757 "\"cannot parse gateway NID '%s'\"", gw);
758 rc = LUSTRE_CFG_RC_BAD_PARAM;
762 /* show all routes with out filtering on gateway */
763 gateway_nid = LNET_NID_ANY;
765 if ((hops < 1 && hops != -1) || hops > 255) {
768 "\"invalid hop count %d, must be between 0 and 256\"",
770 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
774 /* create struct cYAML root object */
775 root = cYAML_create_object(NULL, NULL);
779 route = cYAML_create_seq(root, "route");
784 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
787 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
793 /* filter on provided data */
794 if (net != LNET_NIDNET(LNET_NID_ANY) &&
798 if (gateway_nid != LNET_NID_ANY &&
799 gateway_nid != data.cfg_nid)
803 hops != data.cfg_config_u.cfg_route.rtr_hop)
807 prio != data.cfg_config_u.cfg_route.rtr_priority)
810 /* default rc to -1 incase we hit the goto */
814 item = cYAML_create_seq_item(route);
818 if (first_seq == NULL)
821 if (cYAML_create_string(item, "net",
822 libcfs_net2str(data.cfg_net)) == NULL)
825 if (cYAML_create_string(item, "gateway",
826 libcfs_nid2str(data.cfg_nid)) == NULL)
830 if (cYAML_create_number(item, "hop",
831 data.cfg_config_u.cfg_route.
836 if (cYAML_create_number(item, "priority",
838 cfg_route.rtr_priority) == NULL)
841 if (cYAML_create_string(item, "state",
842 data.cfg_config_u.cfg_route.
844 "up" : "down") == NULL)
849 /* print output iff show_rc is not provided */
851 cYAML_print_tree(root);
853 if (l_errno != ENOENT) {
856 "\"cannot get routes: %s\"",
861 rc = LUSTRE_CFG_RC_NO_ERR;
863 snprintf(err_str, sizeof(err_str), "\"success\"");
865 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
866 cYAML_free_tree(root);
867 } else if (show_rc != NULL && *show_rc != NULL) {
868 struct cYAML *show_node;
869 /* find the route node, if one doesn't exist then
870 * insert one. Otherwise add to the one there
872 show_node = cYAML_get_object_item(*show_rc, "route");
873 if (show_node != NULL && cYAML_is_sequence(show_node)) {
874 cYAML_insert_child(show_node, first_seq);
877 } else if (show_node == NULL) {
878 cYAML_insert_sibling((*show_rc)->cy_child,
882 cYAML_free_tree(root);
888 cYAML_build_error(rc, seq_no, SHOW_CMD, "route", err_str, err_rc);
893 static int socket_intf_query(int request, char *intf,
899 if (strlen(intf) >= IFNAMSIZ || ifr == NULL)
900 return LUSTRE_CFG_RC_BAD_PARAM;
902 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
904 return LUSTRE_CFG_RC_BAD_PARAM;
906 strcpy(ifr->ifr_name, intf);
907 rc = ioctl(sockfd, request, ifr);
909 return LUSTRE_CFG_RC_BAD_PARAM;
915 * for each interface in the array of interfaces find the IP address of
916 * that interface, create its nid and add it to an array of NIDs.
917 * Stop if any of the interfaces is down
919 static int lustre_lnet_intf2nids(struct lnet_dlc_network_descr *nw,
920 lnet_nid_t **nids, __u32 *nnids)
922 int i = 0, count = 0, rc;
925 struct lnet_dlc_intf_descr *intf;
927 if (nw == NULL || nids == NULL)
928 return LUSTRE_CFG_RC_BAD_PARAM;
930 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network)
933 *nids = calloc(count, sizeof(lnet_nid_t));
935 return LUSTRE_CFG_RC_OUT_OF_MEM;
937 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network) {
938 memset(&ifr, 0, sizeof(ifr));
939 rc = socket_intf_query(SIOCGIFFLAGS, intf->intf_name, &ifr);
943 if ((ifr.ifr_flags & IFF_UP) == 0) {
944 rc = LUSTRE_CFG_RC_BAD_PARAM;
948 memset(&ifr, 0, sizeof(ifr));
949 rc = socket_intf_query(SIOCGIFADDR, intf->intf_name, &ifr);
953 ip = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
955 (*nids)[i] = LNET_MKNID(nw->nw_id, ip);
970 * called repeatedly until a match or no more ip range
972 * ip_range expression
973 * interface list with all the interface names.
974 * all the interfaces in the system.
976 * try to match the ip_range expr to one of the interfaces' IPs in
977 * the system. If we hit a patch for an interface. Check if that
978 * interface name is in the list.
980 * If there are more than one interface in the list, then make sure
981 * that the IPs for all of these interfaces match the ip ranges
984 * for each interface in intf_list
985 * look up the intf name in ifa
986 * if not there then no match
987 * check ip obtained from ifa against a match to any of the
989 * If no match, then fail
991 * The result is that all the interfaces have to match.
993 int lustre_lnet_match_ip_to_intf(struct ifaddrs *ifa,
994 struct list_head *intf_list,
995 struct list_head *ip_ranges)
999 struct lnet_dlc_intf_descr *intf_descr;
1000 struct ifaddrs *ifaddr = ifa;
1001 struct lustre_lnet_ip_range_descr *ip_range;
1005 * if there are no explicit interfaces, and no ip ranges, then
1006 * configure the first tcp interface we encounter.
1008 if (list_empty(intf_list) && list_empty(ip_ranges)) {
1009 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1010 if (ifaddr->ifa_addr == NULL)
1013 if ((ifaddr->ifa_flags & IFF_UP) == 0)
1016 family = ifaddr->ifa_addr->sa_family;
1017 if (family == AF_INET) {
1018 rc = lustre_lnet_add_intf_descr
1019 (intf_list, ifaddr->ifa_name,
1020 strlen(ifaddr->ifa_name));
1022 if (rc != LUSTRE_CFG_RC_NO_ERR)
1025 return LUSTRE_CFG_RC_MATCH;
1028 return LUSTRE_CFG_RC_NO_MATCH;
1032 * First interface which matches an IP pattern will be used
1034 if (list_empty(intf_list)) {
1036 * no interfaces provided in the rule, but an ip range is
1037 * provided, so try and match an interface to the ip
1040 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1041 if (ifaddr->ifa_addr == NULL)
1044 if ((ifaddr->ifa_flags & IFF_UP) == 0)
1047 family = ifaddr->ifa_addr->sa_family;
1048 if (family == AF_INET) {
1049 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->
1052 list_for_each_entry(ip_range, ip_ranges,
1054 rc = cfs_ip_addr_match(bswap_32(ip),
1055 &ip_range->ipr_expr);
1059 rc = lustre_lnet_add_intf_descr
1060 (intf_list, ifaddr->ifa_name,
1061 strlen(ifaddr->ifa_name));
1063 if (rc != LUSTRE_CFG_RC_NO_ERR)
1066 return LUSTRE_CFG_RC_MATCH;
1073 * If an interface is explicitly specified the ip-range might or
1074 * might not be specified. if specified the interface needs to match the
1075 * ip-range. If no ip-range then the interfaces are
1076 * automatically matched if they are all up.
1077 * If > 1 interfaces all the interfaces must match for the NI to
1080 list_for_each_entry(intf_descr, intf_list, intf_on_network) {
1081 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1082 if (ifaddr->ifa_addr == NULL)
1085 family = ifaddr->ifa_addr->sa_family;
1086 if (family == AF_INET &&
1087 strcmp(intf_descr->intf_name,
1088 ifaddr->ifa_name) == 0)
1093 return LUSTRE_CFG_RC_NO_MATCH;
1095 if ((ifaddr->ifa_flags & IFF_UP) == 0)
1096 return LUSTRE_CFG_RC_NO_MATCH;
1098 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr.s_addr;
1100 list_for_each_entry(ip_range, ip_ranges, ipr_entry) {
1101 rc = cfs_ip_addr_match(bswap_32(ip), &ip_range->ipr_expr);
1105 return LUSTRE_CFG_RC_NO_MATCH;
1109 return LUSTRE_CFG_RC_MATCH;
1112 int lustre_lnet_resolve_ip2nets_rule(struct lustre_lnet_ip2nets *ip2nets,
1113 lnet_nid_t **nids, __u32 *nnids)
1115 struct ifaddrs *ifa;
1116 int rc = LUSTRE_CFG_RC_NO_ERR;
1118 rc = getifaddrs(&ifa);
1122 rc = lustre_lnet_match_ip_to_intf(ifa,
1123 &ip2nets->ip2nets_net.nw_intflist,
1124 &ip2nets->ip2nets_ip_ranges);
1125 if (rc != LUSTRE_CFG_RC_MATCH) {
1130 rc = lustre_lnet_intf2nids(&ip2nets->ip2nets_net, nids, nnids);
1131 if (rc != LUSTRE_CFG_RC_NO_ERR) {
1142 lustre_lnet_ioctl_config_ni(struct list_head *intf_list,
1143 struct lnet_ioctl_config_lnd_tunables *tunables,
1144 struct cfs_expr_list *global_cpts,
1145 lnet_nid_t *nids, char *err_str)
1148 struct lnet_ioctl_config_ni *conf;
1149 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1150 int rc = LUSTRE_CFG_RC_NO_ERR, i = 0;
1153 struct lnet_dlc_intf_descr *intf_descr;
1155 struct cfs_expr_list *cpt_expr;
1157 list_for_each_entry(intf_descr, intf_list,
1159 if (i == 0 && tunables != NULL)
1160 len = sizeof(struct lnet_ioctl_config_ni) +
1161 sizeof(struct lnet_ioctl_config_lnd_tunables);
1163 len = sizeof(struct lnet_ioctl_config_ni);
1165 data = calloc(1, len);
1166 conf = (struct lnet_ioctl_config_ni*) data;
1167 if (i == 0 && tunables != NULL)
1168 tun = (struct lnet_ioctl_config_lnd_tunables*)
1171 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1172 conf->lic_cfg_hdr.ioc_len = len;
1173 conf->lic_nid = nids[i];
1174 strncpy(conf->lic_ni_intf[0], intf_descr->intf_name,
1177 if (intf_descr->cpt_expr != NULL)
1178 cpt_expr = intf_descr->cpt_expr;
1179 else if (global_cpts != NULL)
1180 cpt_expr = global_cpts;
1184 if (cpt_expr != NULL) {
1185 count = cfs_expr_list_values(cpt_expr,
1186 LNET_MAX_SHOW_NUM_CPT,
1189 memcpy(conf->lic_cpts, cpt_array,
1190 sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1199 conf->lic_ncpts = count;
1201 if (i == 0 && tunables != NULL)
1202 /* TODO put in the LND tunables */
1203 memcpy(tun, tunables, sizeof(*tunables));
1205 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1210 "\"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\"");
1232 if (ip2nets == NULL ||
1233 list_empty(&ip2nets->ip2nets_net.nw_intflist)) {
1236 "\"incomplete ip2nets information\"");
1237 rc = LUSTRE_CFG_RC_BAD_PARAM;
1241 rc = lustre_lnet_resolve_ip2nets_rule(ip2nets, &nids, &nnids);
1242 if (rc != LUSTRE_CFG_RC_NO_ERR && rc != LUSTRE_CFG_RC_MATCH) {
1245 "\"cannot resolve ip2nets rule\"");
1249 rc = lustre_lnet_ioctl_config_ni(&ip2nets->ip2nets_net.nw_intflist,
1250 tunables, global_cpts, nids,
1252 if (rc != LUSTRE_CFG_RC_NO_ERR)
1256 cYAML_build_error(rc, seq_no, ADD_CMD, "ip2nets", err_str, err_rc);
1260 int lustre_lnet_config_ni(struct lnet_dlc_network_descr *nw_descr,
1261 struct cfs_expr_list *global_cpts,
1263 struct lnet_ioctl_config_lnd_tunables *tunables,
1264 int seq_no, struct cYAML **err_rc)
1267 struct lnet_ioctl_config_ni *conf;
1268 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1269 char buf[LNET_MAX_STR_LEN];
1270 int rc = LUSTRE_CFG_RC_NO_ERR;
1271 char err_str[LNET_MAX_STR_LEN];
1272 lnet_nid_t *nids = NULL;
1276 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1279 snprintf(err_str, sizeof(err_str), "\"success\"");
1281 if (ip2net == NULL && nw_descr == NULL) {
1284 "\"mandatory parameters not specified.\"");
1285 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1289 if (ip2net != NULL && strlen(ip2net) >= sizeof(buf)) {
1292 "\"ip2net string too long %d\"",
1293 (int)strlen(ip2net));
1294 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1298 if (ip2net != NULL) {
1299 if (tunables != NULL)
1300 len = sizeof(struct lnet_ioctl_config_ni) +
1301 sizeof(struct lnet_ioctl_config_lnd_tunables);
1303 len = sizeof(struct lnet_ioctl_config_ni);
1304 data = calloc(1, len);
1305 conf = (struct lnet_ioctl_config_ni*) data;
1306 if (tunables != NULL)
1307 tun = (struct lnet_ioctl_config_lnd_tunables*)
1308 (data + sizeof(*conf));
1310 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1311 conf->lic_cfg_hdr.ioc_len = len;
1312 strncpy(conf->lic_legacy_ip2nets, ip2net,
1315 if (global_cpts != NULL) {
1316 count = cfs_expr_list_values(global_cpts,
1317 LNET_MAX_SHOW_NUM_CPT,
1320 memcpy(conf->lic_cpts, cpt_array,
1321 sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1330 conf->lic_ncpts = count;
1332 if (tunables != NULL)
1333 memcpy(tun, tunables, sizeof(*tunables));
1335 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1340 "\"cannot add network: %s\"", strerror(errno));
1347 if (LNET_NETTYP(nw_descr->nw_id) == LOLND)
1348 return LUSTRE_CFG_RC_NO_ERR;
1350 if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1353 "\"cannot parse net '%s'\"",
1354 libcfs_net2str(nw_descr->nw_id));
1355 rc = LUSTRE_CFG_RC_BAD_PARAM;
1359 if (list_empty(&nw_descr->nw_intflist)) {
1362 "\"no interface name provided\"");
1363 rc = LUSTRE_CFG_RC_BAD_PARAM;
1367 rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids);
1369 snprintf(err_str, sizeof(err_str),
1370 "\"bad parameter\"");
1371 rc = LUSTRE_CFG_RC_BAD_PARAM;
1375 rc = lustre_lnet_ioctl_config_ni(&nw_descr->nw_intflist,
1376 tunables, global_cpts, nids,
1380 if (nw_descr != NULL) {
1381 list_for_each_entry_safe(intf_descr, tmp,
1382 &nw_descr->nw_intflist,
1384 if (intf_descr->cpt_expr != NULL)
1385 cfs_expr_list_free(intf_descr->cpt_expr);
1390 cYAML_build_error(rc, seq_no, ADD_CMD, "net", err_str, err_rc);
1401 int lustre_lnet_del_ni(struct lnet_dlc_network_descr *nw_descr,
1402 int seq_no, struct cYAML **err_rc)
1404 struct lnet_ioctl_config_ni data;
1405 int rc = LUSTRE_CFG_RC_NO_ERR, i;
1406 char err_str[LNET_MAX_STR_LEN];
1407 lnet_nid_t *nids = NULL;
1409 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1411 if (LNET_NETTYP(nw_descr->nw_id) == LOLND)
1412 return LUSTRE_CFG_RC_NO_ERR;
1414 snprintf(err_str, sizeof(err_str), "\"success\"");
1416 if (nw_descr == NULL) {
1419 "\"missing mandatory parameter\"");
1420 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1424 if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1427 "\"cannot parse net '%s'\"",
1428 libcfs_net2str(nw_descr->nw_id));
1429 rc = LUSTRE_CFG_RC_BAD_PARAM;
1433 rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids);
1435 snprintf(err_str, sizeof(err_str),
1436 "\"bad parameter\"");
1437 rc = LUSTRE_CFG_RC_BAD_PARAM;
1442 * no interfaces just the nw_id is specified
1445 nids = calloc(1, sizeof(*nids));
1447 snprintf(err_str, sizeof(err_str),
1448 "\"out of memory\"");
1449 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1452 nids[0] = LNET_MKNID(nw_descr->nw_id, 0);
1456 for (i = 0; i < nnids; i++) {
1457 LIBCFS_IOC_INIT_V2(data, lic_cfg_hdr);
1458 data.lic_nid = nids[i];
1460 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_LOCAL_NI, &data);
1465 "\"cannot del network: %s\"", strerror(errno));
1469 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
1471 if (intf_descr->cpt_expr != NULL)
1472 cfs_expr_list_free(intf_descr->cpt_expr);
1477 cYAML_build_error(rc, seq_no, DEL_CMD, "net", err_str, err_rc);
1485 int lustre_lnet_show_net(char *nw, int detail, int seq_no,
1486 struct cYAML **show_rc, struct cYAML **err_rc)
1489 struct lnet_ioctl_config_ni *ni_data;
1490 struct lnet_ioctl_config_lnd_tunables *lnd;
1491 struct lnet_ioctl_element_stats *stats;
1492 __u32 net = LNET_NIDNET(LNET_NID_ANY);
1493 __u32 prev_net = LNET_NIDNET(LNET_NID_ANY);
1494 int rc = LUSTRE_CFG_RC_OUT_OF_MEM, i, j;
1496 struct cYAML *root = NULL, *tunables = NULL,
1497 *net_node = NULL, *interfaces = NULL,
1498 *item = NULL, *first_seq = NULL,
1499 *tmp = NULL, *statistics = NULL;
1500 int str_buf_len = LNET_MAX_SHOW_NUM_CPT * 2;
1501 char str_buf[str_buf_len];
1503 char err_str[LNET_MAX_STR_LEN];
1504 bool exist = false, new_net = true;
1506 size_t buf_size = sizeof(*ni_data) + sizeof(*lnd) + sizeof(*stats);
1508 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1510 buf = calloc(1, buf_size);
1514 ni_data = (struct lnet_ioctl_config_ni *)buf;
1517 net = libcfs_str2net(nw);
1518 if (net == LNET_NIDNET(LNET_NID_ANY)) {
1521 "\"cannot parse net '%s'\"", nw);
1522 rc = LUSTRE_CFG_RC_BAD_PARAM;
1527 root = cYAML_create_object(NULL, NULL);
1531 net_node = cYAML_create_seq(root, "net");
1532 if (net_node == NULL)
1539 memset(buf, 0, buf_size);
1541 LIBCFS_IOC_INIT_V2(*ni_data, lic_cfg_hdr);
1543 * set the ioc_len to the proper value since INIT assumes
1546 ni_data->lic_cfg_hdr.ioc_len = buf_size;
1547 ni_data->lic_idx = i;
1549 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LOCAL_NI, ni_data);
1555 rc_net = LNET_NIDNET(ni_data->lic_nid);
1557 /* filter on provided data */
1558 if (net != LNET_NIDNET(LNET_NID_ANY) &&
1562 /* default rc to -1 in case we hit the goto */
1566 stats = (struct lnet_ioctl_element_stats *)ni_data->lic_bulk;
1567 lnd = (struct lnet_ioctl_config_lnd_tunables *)
1568 (ni_data->lic_bulk + sizeof(*stats));
1570 if (rc_net != prev_net) {
1577 if (!cYAML_create_string(net_node, "net type",
1578 libcfs_net2str(rc_net)))
1581 tmp = cYAML_create_seq(net_node, "local NI(s)");
1587 /* create the tree to be printed. */
1588 item = cYAML_create_seq_item(tmp);
1592 if (first_seq == NULL)
1595 if (cYAML_create_string(item, "nid",
1596 libcfs_nid2str(ni_data->lic_nid)) == NULL)
1599 if (cYAML_create_string(item,
1601 (ni_data->lic_status ==
1602 LNET_NI_STATUS_UP) ?
1603 "up" : "down") == NULL)
1606 /* don't add interfaces unless there is at least one
1608 if (strlen(ni_data->lic_ni_intf[0]) > 0) {
1609 interfaces = cYAML_create_object(item, "interfaces");
1610 if (interfaces == NULL)
1613 for (j = 0; j < LNET_MAX_INTERFACES; j++) {
1614 if (strlen(ni_data->lic_ni_intf[j]) > 0) {
1616 sizeof(str_buf), "%d", j);
1617 if (cYAML_create_string(interfaces,
1619 ni_data->lic_ni_intf[j]) ==
1629 statistics = cYAML_create_object(item, "statistics");
1630 if (statistics == NULL)
1633 if (cYAML_create_number(statistics, "send_count",
1638 if (cYAML_create_number(statistics, "recv_count",
1643 if (cYAML_create_number(statistics, "drop_count",
1648 tunables = cYAML_create_object(item, "tunables");
1652 rc = lustre_net_show_tunables(tunables, &lnd->lt_cmn);
1653 if (rc != LUSTRE_CFG_RC_NO_ERR)
1656 tunables = cYAML_create_object(item, "lnd tunables");
1657 if (tunables == NULL)
1660 rc = lustre_ni_show_tunables(tunables, LNET_NETTYP(rc_net),
1662 if (rc != LUSTRE_CFG_RC_NO_ERR)
1665 if (cYAML_create_number(item, "tcp bonding",
1666 ni_data->lic_tcp_bonding)
1670 if (cYAML_create_number(item, "dev cpt",
1671 ni_data->lic_dev_cpt) == NULL)
1674 /* out put the CPTs in the format: "[x,x,x,...]" */
1675 limit = str_buf + str_buf_len - 3;
1676 pos += snprintf(pos, limit - pos, "\"[");
1677 for (j = 0 ; ni_data->lic_ncpts >= 1 &&
1678 j < ni_data->lic_ncpts &&
1680 pos += snprintf(pos, limit - pos,
1681 "%d", ni_data->lic_cpts[j]);
1682 if ((j + 1) < ni_data->lic_ncpts)
1683 pos += snprintf(pos, limit - pos, ",");
1685 pos += snprintf(pos, 3, "]\"");
1687 if (ni_data->lic_ncpts >= 1 &&
1688 cYAML_create_string(item, "CPT",
1694 /* Print out the net information only if show_rc is not provided */
1695 if (show_rc == NULL)
1696 cYAML_print_tree(root);
1698 if (l_errno != ENOENT) {
1701 "\"cannot get networks: %s\"",
1706 rc = LUSTRE_CFG_RC_NO_ERR;
1708 snprintf(err_str, sizeof(err_str), "\"success\"");
1710 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
1711 cYAML_free_tree(root);
1712 } else if (show_rc != NULL && *show_rc != NULL) {
1713 struct cYAML *show_node;
1714 /* find the net node, if one doesn't exist
1715 * then insert one. Otherwise add to the one there
1717 show_node = cYAML_get_object_item(*show_rc, "net");
1718 if (show_node != NULL && cYAML_is_sequence(show_node)) {
1719 cYAML_insert_child(show_node, first_seq);
1722 } else if (show_node == NULL) {
1723 cYAML_insert_sibling((*show_rc)->cy_child,
1727 cYAML_free_tree(root);
1733 cYAML_build_error(rc, seq_no, SHOW_CMD, "net", err_str, err_rc);
1738 int lustre_lnet_enable_routing(int enable, int seq_no, struct cYAML **err_rc)
1740 struct lnet_ioctl_config_data data;
1741 int rc = LUSTRE_CFG_RC_NO_ERR;
1742 char err_str[LNET_MAX_STR_LEN];
1744 snprintf(err_str, sizeof(err_str), "\"success\"");
1746 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1747 data.cfg_config_u.cfg_buffers.buf_enable = (enable) ? 1 : 0;
1749 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIG_RTR, &data);
1754 "\"cannot %s routing %s\"",
1755 (enable) ? "enable" : "disable", strerror(errno));
1760 cYAML_build_error(rc, seq_no,
1761 (enable) ? ADD_CMD : DEL_CMD,
1762 "routing", err_str, err_rc);
1767 int lustre_lnet_config_numa_range(int range, int seq_no, struct cYAML **err_rc)
1769 struct lnet_ioctl_numa_range data;
1770 int rc = LUSTRE_CFG_RC_NO_ERR;
1771 char err_str[LNET_MAX_STR_LEN];
1773 snprintf(err_str, sizeof(err_str), "\"success\"");
1778 "\"range must be >= 0\"");
1779 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1783 LIBCFS_IOC_INIT_V2(data, nr_hdr);
1784 data.nr_range = range;
1786 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_SET_NUMA_RANGE, &data);
1791 "\"cannot configure buffers: %s\"", strerror(errno));
1796 cYAML_build_error(rc, seq_no, ADD_CMD, "numa_range", err_str, err_rc);
1801 int lustre_lnet_config_buffers(int tiny, int small, int large, int seq_no,
1802 struct cYAML **err_rc)
1804 struct lnet_ioctl_config_data data;
1805 int rc = LUSTRE_CFG_RC_NO_ERR;
1806 char err_str[LNET_MAX_STR_LEN];
1808 snprintf(err_str, sizeof(err_str), "\"success\"");
1810 /* -1 indicates to ignore changes to this field */
1811 if (tiny < -1 || small < -1 || large < -1) {
1814 "\"tiny, small and large must be >= 0\"");
1815 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1819 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1820 data.cfg_config_u.cfg_buffers.buf_tiny = tiny;
1821 data.cfg_config_u.cfg_buffers.buf_small = small;
1822 data.cfg_config_u.cfg_buffers.buf_large = large;
1824 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_BUF, &data);
1829 "\"cannot configure buffers: %s\"", strerror(errno));
1834 cYAML_build_error(rc, seq_no, ADD_CMD, "buf", err_str, err_rc);
1839 int lustre_lnet_show_routing(int seq_no, struct cYAML **show_rc,
1840 struct cYAML **err_rc)
1842 struct lnet_ioctl_config_data *data;
1843 struct lnet_ioctl_pool_cfg *pool_cfg = NULL;
1844 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1847 char *pools[LNET_NRBPOOLS] = {"tiny", "small", "large"};
1848 int buf_count[LNET_NRBPOOLS] = {0};
1849 struct cYAML *root = NULL, *pools_node = NULL,
1850 *type_node = NULL, *item = NULL, *cpt = NULL,
1851 *first_seq = NULL, *buffers = NULL;
1853 char err_str[LNET_MAX_STR_LEN];
1854 char node_name[LNET_MAX_STR_LEN];
1857 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1859 buf = calloc(1, sizeof(*data) + sizeof(*pool_cfg));
1863 data = (struct lnet_ioctl_config_data *)buf;
1865 root = cYAML_create_object(NULL, NULL);
1869 pools_node = cYAML_create_seq(root, "routing");
1870 if (pools_node == NULL)
1874 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
1875 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
1876 sizeof(struct lnet_ioctl_pool_cfg);
1877 data->cfg_count = i;
1879 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_BUF, data);
1887 pool_cfg = (struct lnet_ioctl_pool_cfg *)data->cfg_bulk;
1889 snprintf(node_name, sizeof(node_name), "cpt[%d]", i);
1890 item = cYAML_create_seq_item(pools_node);
1894 if (first_seq == NULL)
1897 cpt = cYAML_create_object(item, node_name);
1901 /* create the tree and print */
1902 for (j = 0; j < LNET_NRBPOOLS; j++) {
1903 type_node = cYAML_create_object(cpt, pools[j]);
1904 if (type_node == NULL)
1906 if (cYAML_create_number(type_node, "npages",
1907 pool_cfg->pl_pools[j].pl_npages)
1910 if (cYAML_create_number(type_node, "nbuffers",
1911 pool_cfg->pl_pools[j].
1912 pl_nbuffers) == NULL)
1914 if (cYAML_create_number(type_node, "credits",
1915 pool_cfg->pl_pools[j].
1916 pl_credits) == NULL)
1918 if (cYAML_create_number(type_node, "mincredits",
1919 pool_cfg->pl_pools[j].
1920 pl_mincredits) == NULL)
1922 /* keep track of the total count for each of the
1923 * tiny, small and large buffers */
1924 buf_count[j] += pool_cfg->pl_pools[j].pl_nbuffers;
1928 if (pool_cfg != NULL) {
1929 item = cYAML_create_seq_item(pools_node);
1933 if (cYAML_create_number(item, "enable", pool_cfg->pl_routing) ==
1938 /* create a buffers entry in the show. This is necessary so that
1939 * if the YAML output is used to configure a node, the buffer
1940 * configuration takes hold */
1941 buffers = cYAML_create_object(root, "buffers");
1942 if (buffers == NULL)
1945 for (i = 0; i < LNET_NRBPOOLS; i++) {
1946 if (cYAML_create_number(buffers, pools[i], buf_count[i]) == NULL)
1950 if (show_rc == NULL)
1951 cYAML_print_tree(root);
1953 if (l_errno != ENOENT) {
1956 "\"cannot get routing information: %s\"",
1961 rc = LUSTRE_CFG_RC_NO_ERR;
1963 snprintf(err_str, sizeof(err_str), "\"success\"");
1964 rc = LUSTRE_CFG_RC_NO_ERR;
1968 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
1969 cYAML_free_tree(root);
1970 } else if (show_rc != NULL && *show_rc != NULL) {
1971 struct cYAML *routing_node;
1972 /* there should exist only one routing block and one
1973 * buffers block. If there already exists a previous one
1974 * then don't add another */
1975 routing_node = cYAML_get_object_item(*show_rc, "routing");
1976 if (routing_node == NULL) {
1977 cYAML_insert_sibling((*show_rc)->cy_child,
1981 cYAML_free_tree(root);
1987 cYAML_build_error(rc, seq_no, SHOW_CMD, "routing", err_str, err_rc);
1992 int lustre_lnet_show_peer(char *knid, int detail, int seq_no,
1993 struct cYAML **show_rc, struct cYAML **err_rc)
1995 struct lnet_ioctl_peer_cfg *peer_info;
1996 struct lnet_peer_ni_credit_info *lpni_cri;
1997 struct lnet_ioctl_element_stats *lpni_stats;
1998 int rc = LUSTRE_CFG_RC_OUT_OF_MEM, ncpt = 0, i = 0, j = 0;
2000 struct cYAML *root = NULL, *peer = NULL, *peer_ni = NULL,
2001 *first_seq = NULL, *peer_root = NULL, *tmp = NULL;
2002 char err_str[LNET_MAX_STR_LEN];
2003 lnet_nid_t prev_primary_nid = LNET_NID_ANY, primary_nid = LNET_NID_ANY;
2004 int data_size = sizeof(*peer_info) + sizeof(*lpni_cri) +
2005 sizeof(*lpni_stats);
2006 char *data = calloc(data_size, 1);
2007 bool new_peer = true;
2009 snprintf(err_str, sizeof(err_str),
2010 "\"out of memory\"");
2015 peer_info = (struct lnet_ioctl_peer_cfg *)data;
2017 /* create struct cYAML root object */
2018 root = cYAML_create_object(NULL, NULL);
2022 peer_root = cYAML_create_seq(root, "peer");
2023 if (peer_root == NULL)
2027 primary_nid = libcfs_str2nid(knid);
2031 memset(data, 0, data_size);
2032 LIBCFS_IOC_INIT_V2(*peer_info, prcfg_hdr);
2033 peer_info->prcfg_hdr.ioc_len = data_size;
2034 peer_info->prcfg_idx = i;
2036 rc = l_ioctl(LNET_DEV_ID,
2037 IOC_LIBCFS_GET_PEER_NI, peer_info);
2043 if (primary_nid != LNET_NID_ANY &&
2044 primary_nid != peer_info->prcfg_key_nid)
2047 lpni_cri = (struct lnet_peer_ni_credit_info*)peer_info->prcfg_bulk;
2048 lpni_stats = (struct lnet_ioctl_element_stats *)
2049 (peer_info->prcfg_bulk +
2052 peer = cYAML_create_seq_item(peer_root);
2056 if (peer_info->prcfg_key_nid != prev_primary_nid) {
2057 prev_primary_nid = peer_info->prcfg_key_nid;
2062 lnet_nid_t pnid = peer_info->prcfg_key_nid;
2063 if (cYAML_create_string(peer, "primary nid",
2064 libcfs_nid2str(pnid))
2067 if (cYAML_create_string(peer, "Multi-Rail",
2068 peer_info->prcfg_mr ?
2072 tmp = cYAML_create_seq(peer, "peer ni");
2078 if (first_seq == NULL)
2081 peer_ni = cYAML_create_seq_item(tmp);
2082 if (peer_ni == NULL)
2085 if (cYAML_create_string(peer_ni, "nid",
2087 (peer_info->prcfg_cfg_nid))
2091 if (cYAML_create_string(peer_ni, "state",
2092 lpni_cri->cr_aliveness)
2099 if (cYAML_create_number(peer_ni, "max_ni_tx_credits",
2100 lpni_cri->cr_ni_peer_tx_credits)
2104 if (cYAML_create_number(peer_ni, "available_tx_credits",
2105 lpni_cri->cr_peer_tx_credits)
2109 if (cYAML_create_number(peer_ni, "min_tx_credits",
2110 lpni_cri->cr_peer_min_tx_credits)
2114 if (cYAML_create_number(peer_ni, "tx_q_num_of_buf",
2115 lpni_cri->cr_peer_tx_qnob)
2119 if (cYAML_create_number(peer_ni, "available_rtr_credits",
2120 lpni_cri->cr_peer_rtr_credits)
2124 if (cYAML_create_number(peer_ni, "min_rtr_credits",
2125 lpni_cri->cr_peer_min_rtr_credits)
2129 if (cYAML_create_number(peer_ni, "send_count",
2130 lpni_stats->send_count)
2134 if (cYAML_create_number(peer_ni, "recv_count",
2135 lpni_stats->recv_count)
2139 if (cYAML_create_number(peer_ni, "drop_count",
2140 lpni_stats->drop_count)
2144 if (cYAML_create_number(peer_ni, "refcount",
2145 lpni_cri->cr_refcount) == NULL)
2149 if (l_errno != ENOENT) {
2152 "\"cannot get peer information: %s\"",
2161 /* print output iff show_rc is not provided */
2162 if (show_rc == NULL)
2163 cYAML_print_tree(root);
2165 snprintf(err_str, sizeof(err_str), "\"success\"");
2166 rc = LUSTRE_CFG_RC_NO_ERR;
2169 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2170 cYAML_free_tree(root);
2171 } else if (show_rc != NULL && *show_rc != NULL) {
2172 struct cYAML *show_node;
2173 /* find the peer node, if one doesn't exist then
2174 * insert one. Otherwise add to the one there
2176 show_node = cYAML_get_object_item(*show_rc,
2178 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2179 cYAML_insert_child(show_node, first_seq);
2182 } else if (show_node == NULL) {
2183 cYAML_insert_sibling((*show_rc)->cy_child,
2187 cYAML_free_tree(root);
2193 cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
2199 int lustre_lnet_show_numa_range(int seq_no, struct cYAML **show_rc,
2200 struct cYAML **err_rc)
2202 struct lnet_ioctl_numa_range data;
2203 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2205 char err_str[LNET_MAX_STR_LEN];
2206 struct cYAML *root = NULL, *range = NULL;
2208 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2210 LIBCFS_IOC_INIT_V2(data, nr_hdr);
2212 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NUMA_RANGE, &data);
2217 "\"cannot get numa range: %s\"",
2223 root = cYAML_create_object(NULL, NULL);
2227 range = cYAML_create_object(root, "numa");
2231 if (cYAML_create_number(range, "range",
2232 data.nr_range) == NULL)
2235 if (show_rc == NULL)
2236 cYAML_print_tree(root);
2238 snprintf(err_str, sizeof(err_str), "\"success\"");
2240 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2241 cYAML_free_tree(root);
2242 } else if (show_rc != NULL && *show_rc != NULL) {
2243 cYAML_insert_sibling((*show_rc)->cy_child,
2250 cYAML_build_error(rc, seq_no, SHOW_CMD, "numa", err_str, err_rc);
2255 int lustre_lnet_show_stats(int seq_no, struct cYAML **show_rc,
2256 struct cYAML **err_rc)
2258 struct lnet_ioctl_lnet_stats data;
2259 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2261 char err_str[LNET_MAX_STR_LEN];
2262 struct cYAML *root = NULL, *stats = NULL;
2264 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2266 LIBCFS_IOC_INIT_V2(data, st_hdr);
2268 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LNET_STATS, &data);
2273 "\"cannot get lnet statistics: %s\"",
2279 root = cYAML_create_object(NULL, NULL);
2283 stats = cYAML_create_object(root, "statistics");
2287 if (cYAML_create_number(stats, "msgs_alloc",
2288 data.st_cntrs.msgs_alloc) == NULL)
2291 if (cYAML_create_number(stats, "msgs_max",
2292 data.st_cntrs.msgs_max) == NULL)
2295 if (cYAML_create_number(stats, "errors",
2296 data.st_cntrs.errors) == NULL)
2299 if (cYAML_create_number(stats, "send_count",
2300 data.st_cntrs.send_count) == NULL)
2303 if (cYAML_create_number(stats, "recv_count",
2304 data.st_cntrs.recv_count) == NULL)
2307 if (cYAML_create_number(stats, "route_count",
2308 data.st_cntrs.route_count) == NULL)
2311 if (cYAML_create_number(stats, "drop_count",
2312 data.st_cntrs.drop_count) == NULL)
2315 if (cYAML_create_number(stats, "send_length",
2316 data.st_cntrs.send_length) == NULL)
2319 if (cYAML_create_number(stats, "recv_length",
2320 data.st_cntrs.recv_length) == NULL)
2323 if (cYAML_create_number(stats, "route_length",
2324 data.st_cntrs.route_length) == NULL)
2327 if (cYAML_create_number(stats, "drop_length",
2328 data.st_cntrs.drop_length) == NULL)
2331 if (show_rc == NULL)
2332 cYAML_print_tree(root);
2334 snprintf(err_str, sizeof(err_str), "\"success\"");
2336 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2337 cYAML_free_tree(root);
2338 } else if (show_rc != NULL && *show_rc != NULL) {
2339 cYAML_insert_sibling((*show_rc)->cy_child,
2346 cYAML_build_error(rc, seq_no, SHOW_CMD, "statistics", err_str, err_rc);
2351 typedef int (*cmd_handler_t)(struct cYAML *tree,
2352 struct cYAML **show_rc,
2353 struct cYAML **err_rc);
2355 static int handle_yaml_config_route(struct cYAML *tree, struct cYAML **show_rc,
2356 struct cYAML **err_rc)
2358 struct cYAML *net, *gw, *hop, *prio, *seq_no;
2360 net = cYAML_get_object_item(tree, "net");
2361 gw = cYAML_get_object_item(tree, "gateway");
2362 hop = cYAML_get_object_item(tree, "hop");
2363 prio = cYAML_get_object_item(tree, "priority");
2364 seq_no = cYAML_get_object_item(tree, "seq_no");
2366 return lustre_lnet_config_route((net) ? net->cy_valuestring : NULL,
2367 (gw) ? gw->cy_valuestring : NULL,
2368 (hop) ? hop->cy_valueint : -1,
2369 (prio) ? prio->cy_valueint : -1,
2370 (seq_no) ? seq_no->cy_valueint : -1,
2374 static void yaml_free_string_array(char **array, int num)
2377 char **sub_array = array;
2379 for (i = 0; i < num; i++) {
2380 if (*sub_array != NULL)
2390 * 0: <intf_name>['['<expr>']']
2391 * 1: <intf_name>['['<expr>']']
2393 static int yaml_copy_intf_info(struct cYAML *intf_tree,
2394 struct lnet_dlc_network_descr *nw_descr)
2396 struct cYAML *child = NULL;
2397 int intf_num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
2398 struct lnet_dlc_intf_descr *intf_descr, *tmp;
2400 if (intf_tree == NULL || nw_descr == NULL)
2401 return LUSTRE_CFG_RC_BAD_PARAM;
2403 /* now grab all the interfaces and their cpts */
2404 child = intf_tree->cy_child;
2405 while (child != NULL) {
2406 if (child->cy_valuestring == NULL) {
2407 child = child->cy_next;
2411 if (strlen(child->cy_valuestring) >= LNET_MAX_STR_LEN)
2414 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist,
2415 child->cy_valuestring,
2416 strlen(child->cy_valuestring));
2417 if (rc != LUSTRE_CFG_RC_NO_ERR)
2421 child = child->cy_next;
2425 return LUSTRE_CFG_RC_MISSING_PARAM;
2430 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
2432 if (intf_descr->cpt_expr != NULL)
2433 cfs_expr_list_free(intf_descr->cpt_expr);
2441 yaml_extract_cmn_tunables(struct cYAML *tree,
2442 struct lnet_ioctl_config_lnd_cmn_tunables *tunables,
2443 struct cfs_expr_list **global_cpts)
2445 struct cYAML *tun, *item, *smp;
2448 tun = cYAML_get_object_item(tree, "tunables");
2450 item = cYAML_get_object_item(tun, "peer_timeout");
2452 tunables->lct_peer_timeout = item->cy_valueint;
2453 item = cYAML_get_object_item(tun, "peer_credits");
2455 tunables->lct_peer_tx_credits = item->cy_valueint;
2456 item = cYAML_get_object_item(tun, "peer_buffer_credits");
2458 tunables->lct_peer_rtr_credits = item->cy_valueint;
2459 item = cYAML_get_object_item(tun, "credits");
2461 tunables->lct_max_tx_credits = item->cy_valueint;
2462 smp = cYAML_get_object_item(tun, "CPT");
2464 rc = cfs_expr_list_parse(smp->cy_valuestring,
2465 strlen(smp->cy_valuestring),
2466 0, UINT_MAX, global_cpts);
2468 *global_cpts = NULL;
2478 yaml_extract_tunables(struct cYAML *tree,
2479 struct lnet_ioctl_config_lnd_tunables *tunables,
2480 struct cfs_expr_list **global_cpts,
2485 rc = yaml_extract_cmn_tunables(tree, &tunables->lt_cmn,
2491 lustre_yaml_extract_lnd_tunables(tree, net_type,
2499 * - net type: <net>[<NUM>]
2501 * - nid: <ip>@<net>[<NUM>]
2504 * 0: <intf_name>['['<expr>']']
2505 * 1: <intf_name>['['<expr>']']
2507 * peer_timeout: <NUM>
2508 * peer_credits: <NUM>
2509 * peer_buffer_credits: <NUM>
2512 * peercredits_hiw: <NUM>
2513 * map_on_demand: <NUM>
2514 * concurrent_sends: <NUM>
2515 * fmr_pool_size: <NUM>
2516 * fmr_flush_trigger: <NUM>
2519 * At least one interface is required. If no interfaces are provided the
2520 * network interface can not be configured.
2522 static int handle_yaml_config_ni(struct cYAML *tree, struct cYAML **show_rc,
2523 struct cYAML **err_rc)
2525 struct cYAML *net, *intf, *seq_no, *ip2net = NULL, *local_nis = NULL,
2527 int num_entries = 0, rc;
2528 struct lnet_dlc_network_descr nw_descr;
2529 struct cfs_expr_list *global_cpts = NULL;
2530 struct lnet_ioctl_config_lnd_tunables tunables;
2533 memset(&tunables, 0, sizeof(tunables));
2535 INIT_LIST_HEAD(&nw_descr.network_on_rule);
2536 INIT_LIST_HEAD(&nw_descr.nw_intflist);
2538 ip2net = cYAML_get_object_item(tree, "ip2net");
2539 net = cYAML_get_object_item(tree, "net type");
2541 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
2544 * if neither net nor ip2nets are present, then we can not
2545 * configure the network.
2547 if (!net && !ip2net)
2548 return LUSTRE_CFG_RC_MISSING_PARAM;
2550 local_nis = cYAML_get_object_item(tree, "local NI(s)");
2551 if (local_nis == NULL)
2552 return LUSTRE_CFG_RC_MISSING_PARAM;
2554 if (!cYAML_is_sequence(local_nis))
2555 return LUSTRE_CFG_RC_BAD_PARAM;
2557 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
2558 intf = cYAML_get_object_item(item, "interfaces");
2561 num_entries = yaml_copy_intf_info(intf, &nw_descr);
2562 if (num_entries <= 0) {
2563 cYAML_build_error(num_entries, -1, "ni", "add",
2564 "bad interface list",
2566 return LUSTRE_CFG_RC_BAD_PARAM;
2570 found = yaml_extract_tunables(tree, &tunables, &global_cpts,
2571 LNET_NETTYP(nw_descr.nw_id));
2572 seq_no = cYAML_get_object_item(tree, "seq_no");
2574 rc = lustre_lnet_config_ni(&nw_descr,
2576 (ip2net) ? ip2net->cy_valuestring : NULL,
2577 (found) ? &tunables: NULL,
2578 (seq_no) ? seq_no->cy_valueint : -1,
2581 if (global_cpts != NULL)
2582 cfs_expr_list_free(global_cpts);
2589 * - net-spec: <tcp|o2ib|gni>[NUM]
2591 * 0: <intf name>['['<expr>']']
2592 * 1: <intf name>['['<expr>']']
2594 * 0: <expr.expr.expr.expr>
2595 * 1: <expr.expr.expr.expr>
2597 static int handle_yaml_config_ip2nets(struct cYAML *tree,
2598 struct cYAML **show_rc,
2599 struct cYAML **err_rc)
2601 struct cYAML *net, *ip_range, *item = NULL, *intf = NULL,
2603 struct lustre_lnet_ip2nets ip2nets;
2604 struct lustre_lnet_ip_range_descr *ip_range_descr = NULL,
2606 int rc = LUSTRE_CFG_RC_NO_ERR;
2607 struct cfs_expr_list *global_cpts = NULL;
2608 struct cfs_expr_list *el, *el_tmp;
2609 struct lnet_ioctl_config_lnd_tunables tunables;
2610 struct lnet_dlc_intf_descr *intf_descr, *intf_tmp;
2613 memset(&tunables, 0, sizeof(tunables));
2615 /* initialize all lists */
2616 INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
2617 INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
2618 INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
2620 net = cYAML_get_object_item(tree, "net-spec");
2622 return LUSTRE_CFG_RC_BAD_PARAM;
2624 if (net != NULL && net->cy_valuestring == NULL)
2625 return LUSTRE_CFG_RC_BAD_PARAM;
2627 /* assign the network id */
2628 ip2nets.ip2nets_net.nw_id = libcfs_str2net(net->cy_valuestring);
2629 if (ip2nets.ip2nets_net.nw_id == LNET_NID_ANY)
2630 return LUSTRE_CFG_RC_BAD_PARAM;
2632 seq_no = cYAML_get_object_item(tree, "seq_no");
2634 intf = cYAML_get_object_item(tree, "interfaces");
2636 rc = yaml_copy_intf_info(intf, &ip2nets.ip2nets_net);
2638 return LUSTRE_CFG_RC_BAD_PARAM;
2641 ip_range = cYAML_get_object_item(tree, "ip-range");
2642 if (ip_range != NULL) {
2643 item = ip_range->cy_child;
2644 while (item != NULL) {
2645 if (item->cy_valuestring == NULL) {
2646 item = item->cy_next;
2650 rc = lustre_lnet_add_ip_range(&ip2nets.ip2nets_ip_ranges,
2651 item->cy_valuestring);
2653 if (rc != LUSTRE_CFG_RC_NO_ERR)
2656 item = item->cy_next;
2660 found = yaml_extract_tunables(tree, &tunables, &global_cpts,
2661 LNET_NETTYP(ip2nets.ip2nets_net.nw_id));
2663 rc = lustre_lnet_config_ip2nets(&ip2nets,
2664 (found) ? &tunables : NULL,
2666 (seq_no) ? seq_no->cy_valueint : -1,
2670 * don't stop because there was no match. Continue processing the
2671 * rest of the rules. If non-match then nothing is configured
2673 if (rc == LUSTRE_CFG_RC_NO_MATCH)
2674 rc = LUSTRE_CFG_RC_NO_ERR;
2676 list_for_each_entry_safe(intf_descr, intf_tmp,
2677 &ip2nets.ip2nets_net.nw_intflist,
2679 if (intf_descr->cpt_expr != NULL)
2680 cfs_expr_list_free(intf_descr->cpt_expr);
2684 list_for_each_entry_safe(ip_range_descr, tmp,
2685 &ip2nets.ip2nets_ip_ranges,
2687 list_for_each_entry_safe(el, el_tmp, &ip_range_descr->ipr_expr,
2689 cfs_expr_list_free(el);
2690 free(ip_range_descr);
2696 static int handle_yaml_del_ni(struct cYAML *tree, struct cYAML **show_rc,
2697 struct cYAML **err_rc)
2699 struct cYAML *net = NULL, *intf = NULL, *seq_no = NULL, *item = NULL,
2701 int num_entries, rc;
2702 struct lnet_dlc_network_descr nw_descr;
2704 INIT_LIST_HEAD(&nw_descr.network_on_rule);
2705 INIT_LIST_HEAD(&nw_descr.nw_intflist);
2707 net = cYAML_get_object_item(tree, "net type");
2709 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
2711 local_nis = cYAML_get_object_item(tree, "local NI(s)");
2712 if (local_nis == NULL)
2713 return LUSTRE_CFG_RC_MISSING_PARAM;
2715 if (!cYAML_is_sequence(local_nis))
2716 return LUSTRE_CFG_RC_BAD_PARAM;
2718 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
2719 intf = cYAML_get_object_item(item, "interfaces");
2722 num_entries = yaml_copy_intf_info(intf, &nw_descr);
2723 if (num_entries <= 0) {
2724 cYAML_build_error(num_entries, -1, "ni", "add",
2725 "bad interface list",
2727 return LUSTRE_CFG_RC_BAD_PARAM;
2731 seq_no = cYAML_get_object_item(tree, "seq_no");
2733 rc = lustre_lnet_del_ni((net) ? &nw_descr : NULL,
2734 (seq_no) ? seq_no->cy_valueint : -1,
2740 static int yaml_copy_peer_nids(struct cYAML *tree, char ***nidsppp)
2742 struct cYAML *nids_entry = NULL, *child = NULL, *entry = NULL;
2744 int num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
2746 nids_entry = cYAML_get_object_item(tree, "peer ni");
2747 if (cYAML_is_sequence(nids_entry)) {
2748 while (cYAML_get_next_seq_item(nids_entry, &child))
2753 return LUSTRE_CFG_RC_MISSING_PARAM;
2755 nids = calloc(sizeof(*nids) * num, 1);
2757 return LUSTRE_CFG_RC_OUT_OF_MEM;
2759 /* now grab all the nids */
2762 while (cYAML_get_next_seq_item(nids_entry, &child)) {
2763 entry = cYAML_get_object_item(child, "nid");
2766 nids[num] = calloc(strlen(entry->cy_valuestring) + 1, 1);
2768 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2771 strncpy(nids[num], entry->cy_valuestring,
2772 strlen(entry->cy_valuestring));
2782 yaml_free_string_array(nids, num);
2787 static int handle_yaml_config_peer(struct cYAML *tree, struct cYAML **show_rc,
2788 struct cYAML **err_rc)
2792 struct cYAML *seq_no, *key_nid, *non_mr;
2794 num = yaml_copy_peer_nids(tree, &nids);
2798 seq_no = cYAML_get_object_item(tree, "seq_no");
2799 key_nid = cYAML_get_object_item(tree, "primary nid");
2800 non_mr = cYAML_get_object_item(tree, "non_mr");
2802 rc = lustre_lnet_config_peer_nid((key_nid) ? key_nid->cy_valuestring : NULL,
2804 (non_mr) ? false : true,
2805 (seq_no) ? seq_no->cy_valueint : -1,
2808 yaml_free_string_array(nids, num);
2812 static int handle_yaml_del_peer(struct cYAML *tree, struct cYAML **show_rc,
2813 struct cYAML **err_rc)
2817 struct cYAML *seq_no, *key_nid;
2819 num = yaml_copy_peer_nids(tree, &nids);
2823 seq_no = cYAML_get_object_item(tree, "seq_no");
2824 key_nid = cYAML_get_object_item(tree, "primary nid");
2826 rc = lustre_lnet_del_peer_nid((key_nid) ? key_nid->cy_valuestring : NULL,
2828 (seq_no) ? seq_no->cy_valueint : -1,
2831 yaml_free_string_array(nids, num);
2835 static int handle_yaml_config_buffers(struct cYAML *tree,
2836 struct cYAML **show_rc,
2837 struct cYAML **err_rc)
2840 struct cYAML *tiny, *small, *large, *seq_no;
2842 tiny = cYAML_get_object_item(tree, "tiny");
2843 small = cYAML_get_object_item(tree, "small");
2844 large = cYAML_get_object_item(tree, "large");
2845 seq_no = cYAML_get_object_item(tree, "seq_no");
2847 rc = lustre_lnet_config_buffers((tiny) ? tiny->cy_valueint : -1,
2848 (small) ? small->cy_valueint : -1,
2849 (large) ? large->cy_valueint : -1,
2850 (seq_no) ? seq_no->cy_valueint : -1,
2856 static int handle_yaml_config_routing(struct cYAML *tree,
2857 struct cYAML **show_rc,
2858 struct cYAML **err_rc)
2860 int rc = LUSTRE_CFG_RC_NO_ERR;
2861 struct cYAML *seq_no, *enable;
2863 seq_no = cYAML_get_object_item(tree, "seq_no");
2864 enable = cYAML_get_object_item(tree, "enable");
2867 rc = lustre_lnet_enable_routing(enable->cy_valueint,
2869 seq_no->cy_valueint : -1,
2876 static int handle_yaml_del_route(struct cYAML *tree, struct cYAML **show_rc,
2877 struct cYAML **err_rc)
2881 struct cYAML *seq_no;
2883 net = cYAML_get_object_item(tree, "net");
2884 gw = cYAML_get_object_item(tree, "gateway");
2885 seq_no = cYAML_get_object_item(tree, "seq_no");
2887 return lustre_lnet_del_route((net) ? net->cy_valuestring : NULL,
2888 (gw) ? gw->cy_valuestring : NULL,
2889 (seq_no) ? seq_no->cy_valueint : -1,
2893 static int handle_yaml_del_routing(struct cYAML *tree, struct cYAML **show_rc,
2894 struct cYAML **err_rc)
2896 struct cYAML *seq_no;
2898 seq_no = cYAML_get_object_item(tree, "seq_no");
2900 return lustre_lnet_enable_routing(0, (seq_no) ?
2901 seq_no->cy_valueint : -1,
2905 static int handle_yaml_show_route(struct cYAML *tree, struct cYAML **show_rc,
2906 struct cYAML **err_rc)
2912 struct cYAML *detail;
2913 struct cYAML *seq_no;
2915 net = cYAML_get_object_item(tree, "net");
2916 gw = cYAML_get_object_item(tree, "gateway");
2917 hop = cYAML_get_object_item(tree, "hop");
2918 prio = cYAML_get_object_item(tree, "priority");
2919 detail = cYAML_get_object_item(tree, "detail");
2920 seq_no = cYAML_get_object_item(tree, "seq_no");
2922 return lustre_lnet_show_route((net) ? net->cy_valuestring : NULL,
2923 (gw) ? gw->cy_valuestring : NULL,
2924 (hop) ? hop->cy_valueint : -1,
2925 (prio) ? prio->cy_valueint : -1,
2926 (detail) ? detail->cy_valueint : 0,
2927 (seq_no) ? seq_no->cy_valueint : -1,
2932 static int handle_yaml_show_net(struct cYAML *tree, struct cYAML **show_rc,
2933 struct cYAML **err_rc)
2935 struct cYAML *net, *detail, *seq_no;
2937 net = cYAML_get_object_item(tree, "net");
2938 detail = cYAML_get_object_item(tree, "detail");
2939 seq_no = cYAML_get_object_item(tree, "seq_no");
2941 return lustre_lnet_show_net((net) ? net->cy_valuestring : NULL,
2942 (detail) ? detail->cy_valueint : 0,
2943 (seq_no) ? seq_no->cy_valueint : -1,
2948 static int handle_yaml_show_routing(struct cYAML *tree, struct cYAML **show_rc,
2949 struct cYAML **err_rc)
2951 struct cYAML *seq_no;
2953 seq_no = cYAML_get_object_item(tree, "seq_no");
2955 return lustre_lnet_show_routing((seq_no) ? seq_no->cy_valueint : -1,
2959 static int handle_yaml_show_credits(struct cYAML *tree, struct cYAML **show_rc,
2960 struct cYAML **err_rc)
2962 struct cYAML *seq_no, *key_nid, *detail;
2964 seq_no = cYAML_get_object_item(tree, "seq_no");
2965 detail = cYAML_get_object_item(tree, "detail");
2966 key_nid = cYAML_get_object_item(tree, "key_nid");
2968 return lustre_lnet_show_peer((key_nid) ? key_nid->cy_valuestring : NULL,
2969 (detail) ? detail->cy_valueint : 0,
2970 (seq_no) ? seq_no->cy_valueint : -1,
2974 static int handle_yaml_show_stats(struct cYAML *tree, struct cYAML **show_rc,
2975 struct cYAML **err_rc)
2977 struct cYAML *seq_no;
2979 seq_no = cYAML_get_object_item(tree, "seq_no");
2981 return lustre_lnet_show_stats((seq_no) ? seq_no->cy_valueint : -1,
2985 static int handle_yaml_config_numa(struct cYAML *tree, struct cYAML **show_rc,
2986 struct cYAML **err_rc)
2988 struct cYAML *seq_no, *range;
2990 seq_no = cYAML_get_object_item(tree, "seq_no");
2991 range = cYAML_get_object_item(tree, "range");
2993 return lustre_lnet_config_numa_range(range ? range->cy_valueint : -1,
2994 seq_no ? seq_no->cy_valueint : -1,
2998 static int handle_yaml_del_numa(struct cYAML *tree, struct cYAML **show_rc,
2999 struct cYAML **err_rc)
3001 struct cYAML *seq_no;
3003 seq_no = cYAML_get_object_item(tree, "seq_no");
3005 return lustre_lnet_config_numa_range(0, seq_no ? seq_no->cy_valueint : -1,
3009 static int handle_yaml_show_numa(struct cYAML *tree, struct cYAML **show_rc,
3010 struct cYAML **err_rc)
3012 struct cYAML *seq_no;
3014 seq_no = cYAML_get_object_item(tree, "seq_no");
3016 return lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint : -1,
3020 struct lookup_cmd_hdlr_tbl {
3025 static struct lookup_cmd_hdlr_tbl lookup_config_tbl[] = {
3026 {"route", handle_yaml_config_route},
3027 {"net", handle_yaml_config_ni},
3028 {"ip2nets", handle_yaml_config_ip2nets},
3029 {"peer", handle_yaml_config_peer},
3030 {"routing", handle_yaml_config_routing},
3031 {"buffers", handle_yaml_config_buffers},
3032 {"numa", handle_yaml_config_numa},
3036 static struct lookup_cmd_hdlr_tbl lookup_del_tbl[] = {
3037 {"route", handle_yaml_del_route},
3038 {"net", handle_yaml_del_ni},
3039 {"peer", handle_yaml_del_peer},
3040 {"routing", handle_yaml_del_routing},
3041 {"numa", handle_yaml_del_numa},
3045 static struct lookup_cmd_hdlr_tbl lookup_show_tbl[] = {
3046 {"route", handle_yaml_show_route},
3047 {"net", handle_yaml_show_net},
3048 {"buffers", handle_yaml_show_routing},
3049 {"routing", handle_yaml_show_routing},
3050 {"credits", handle_yaml_show_credits},
3051 {"statistics", handle_yaml_show_stats},
3052 {"numa", handle_yaml_show_numa},
3056 static cmd_handler_t lookup_fn(char *key,
3057 struct lookup_cmd_hdlr_tbl *tbl)
3063 for (i = 0; tbl[i].name != NULL; i++) {
3064 if (strncmp(key, tbl[i].name, strlen(tbl[i].name)) == 0)
3071 static int lustre_yaml_cb_helper(char *f, struct lookup_cmd_hdlr_tbl *table,
3072 struct cYAML **show_rc, struct cYAML **err_rc)
3074 struct cYAML *tree, *item = NULL, *head, *child;
3076 char err_str[LNET_MAX_STR_LEN];
3077 int rc = LUSTRE_CFG_RC_NO_ERR, return_rc = LUSTRE_CFG_RC_NO_ERR;
3079 tree = cYAML_build_tree(f, NULL, 0, err_rc, false);
3081 return LUSTRE_CFG_RC_BAD_PARAM;
3083 child = tree->cy_child;
3084 while (child != NULL) {
3085 cb = lookup_fn(child->cy_string, table);
3087 snprintf(err_str, sizeof(err_str),
3088 "\"call back for '%s' not found\"",
3090 cYAML_build_error(LUSTRE_CFG_RC_BAD_PARAM, -1,
3091 "yaml", "helper", err_str, err_rc);
3095 if (cYAML_is_sequence(child)) {
3096 while ((head = cYAML_get_next_seq_item(child, &item))
3098 rc = cb(head, show_rc, err_rc);
3099 if (rc != LUSTRE_CFG_RC_NO_ERR)
3103 rc = cb(child, show_rc, err_rc);
3104 if (rc != LUSTRE_CFG_RC_NO_ERR)
3108 child = child->cy_next;
3112 cYAML_free_tree(tree);
3117 int lustre_yaml_config(char *f, struct cYAML **err_rc)
3119 return lustre_yaml_cb_helper(f, lookup_config_tbl,
3123 int lustre_yaml_del(char *f, struct cYAML **err_rc)
3125 return lustre_yaml_cb_helper(f, lookup_del_tbl,
3129 int lustre_yaml_show(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
3131 return lustre_yaml_cb_helper(f, lookup_show_tbl,
3135 int lustre_lnet_send_dbg_task(enum lnet_dbg_task dbg_task,
3136 struct lnet_dbg_task_info *dbg_info,
3137 struct cYAML **show_rc,
3138 struct cYAML **err_rc)
3140 struct lnet_ioctl_dbg *dbg;
3141 struct lnet_dbg_task_info *info;
3142 int rc = LUSTRE_CFG_RC_NO_ERR;
3143 char err_str[LNET_MAX_STR_LEN];
3145 snprintf(err_str, sizeof(err_str), "\"success\"");
3147 dbg = calloc(1, sizeof(*dbg) + sizeof(*info));
3149 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3150 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3154 info = (struct lnet_dbg_task_info *)dbg->dbg_bulk;
3156 LIBCFS_IOC_INIT_V2(*dbg, dbg_hdr);
3158 dbg->dbg_task = dbg_task;
3160 memcpy(info, dbg_info, sizeof(*info));
3162 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DBG, dbg);
3167 "\"debug task failed %s\"", strerror(errno));
3172 cYAML_build_error(rc, -1, DBG_CMD,
3173 "debug", err_str, err_rc);