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 <sys/types.h>
52 #include "liblnetconfig.h"
55 #define CONFIG_CMD "configure"
56 #define UNCONFIG_CMD "unconfigure"
59 #define SHOW_CMD "show"
63 * lustre_lnet_ip_range_descr
64 * Describes an IP range.
65 * Each octect is an expression
67 struct lustre_lnet_ip_range_descr {
68 struct list_head ipr_entry;
69 struct list_head ipr_expr;
74 * Describes an ip2nets rule. This can be on a list of rules.
76 struct lustre_lnet_ip2nets {
77 struct lnet_dlc_network_descr ip2nets_net;
78 struct list_head ip2nets_ip_ranges;
82 * lustre_lnet_add_ip_range
84 * given a string of the format:
85 * <expr.expr.expr.expr> parse each expr into
86 * a lustre_lnet_ip_range_descr structure and insert on the list.
88 * This function is called from
89 * YAML on each ip-range.
90 * As a result of lnetctl command
91 * When building a NID or P2P selection rules
93 int lustre_lnet_add_ip_range(struct list_head *list, char *str_ip_range)
95 struct lustre_lnet_ip_range_descr *ip_range;
98 ip_range = calloc(1, sizeof(*ip_range));
100 return LUSTRE_CFG_RC_OUT_OF_MEM;
102 INIT_LIST_HEAD(&ip_range->ipr_entry);
103 INIT_LIST_HEAD(&ip_range->ipr_expr);
105 rc = cfs_ip_addr_parse(str_ip_range, strlen(str_ip_range),
106 &ip_range->ipr_expr);
108 return LUSTRE_CFG_RC_BAD_PARAM;
110 list_add_tail(&ip_range->ipr_entry, list);
112 return LUSTRE_CFG_RC_NO_ERR;
115 int lustre_lnet_add_intf_descr(struct list_head *list, char *intf, int len)
117 char *open_sq_bracket = NULL, *close_sq_bracket = NULL,
119 struct lnet_dlc_intf_descr *intf_descr = NULL;
121 char intf_string[LNET_MAX_STR_LEN];
123 if (len >= LNET_MAX_STR_LEN)
124 return LUSTRE_CFG_RC_BAD_PARAM;
126 strncpy(intf_string, intf, len);
127 intf_string[len] = '\0';
129 intf_descr = calloc(1, sizeof(*intf_descr));
130 if (intf_descr == NULL)
131 return LUSTRE_CFG_RC_OUT_OF_MEM;
133 INIT_LIST_HEAD(&intf_descr->intf_on_network);
135 intf_name = intf_string;
136 open_sq_bracket = strchr(intf_string, '[');
137 if (open_sq_bracket != NULL) {
138 close_sq_bracket = strchr(intf_string, ']');
139 if (close_sq_bracket == NULL) {
141 return LUSTRE_CFG_RC_BAD_PARAM;
143 rc = cfs_expr_list_parse(open_sq_bracket,
144 strlen(open_sq_bracket), 0, UINT_MAX,
145 &intf_descr->cpt_expr);
148 return LUSTRE_CFG_RC_BAD_PARAM;
150 strncpy(intf_descr->intf_name, intf_name,
151 open_sq_bracket - intf_name);
152 intf_descr->intf_name[open_sq_bracket - intf_name] = '\0';
154 strcpy(intf_descr->intf_name, intf_name);
155 intf_descr->cpt_expr = NULL;
158 list_add_tail(&intf_descr->intf_on_network, list);
160 return LUSTRE_CFG_RC_NO_ERR;
163 void lustre_lnet_init_nw_descr(struct lnet_dlc_network_descr *nw_descr)
165 if (nw_descr != NULL) {
166 INIT_LIST_HEAD(&nw_descr->network_on_rule);
167 INIT_LIST_HEAD(&nw_descr->nw_intflist);
171 int lustre_lnet_parse_nids(char *nids, char **array, int size,
175 char *comma = nids, *cur, *entry;
177 int i, len, start = 0, finish = 0;
179 if (nids == NULL || strlen(nids) == 0)
182 /* count the number or new nids, by counting the number of commas */
184 comma = strchr(comma, ',');
194 * if the array is not NULL allocate a large enough array to house
195 * the old and new entries
197 new_array = calloc(sizeof(char*),
198 (size > 0) ? size + num_nids : num_nids);
203 /* parse our the new nids and add them to the tail of the array */
206 start = (size > 0) ? size: 0;
207 finish = (size > 0) ? size + num_nids : num_nids;
208 for (i = start; i < finish; i++) {
209 comma = strchr(comma, ',');
212 * the length of the string to be parsed out is
213 * from cur to end of string. So it's good enough
216 len = strlen(cur) + 1;
218 /* length of the string is comma - cur */
219 len = (comma - cur) + 1;
221 entry = calloc(1, len);
223 finish = i > 0 ? i - 1: 0;
226 strncpy(entry, cur, len - 1);
228 new_array[i] = entry;
235 /* add the old entries in the array and delete the old array*/
236 for (i = 0; i < size; i++)
237 new_array[i] = array[i];
242 *out_array = new_array;
247 for (i = start; i < finish; i++)
257 * <intf>[<expr>], <intf>[<expr>],..
259 int lustre_lnet_parse_interfaces(char *intf_str,
260 struct lnet_dlc_network_descr *nw_descr)
265 char *cur = intf_str, *next = NULL;
266 char *end = intf_str + strlen(intf_str);
268 struct lnet_dlc_intf_descr *intf_descr, *tmp;
270 if (nw_descr == NULL)
271 return LUSTRE_CFG_RC_BAD_PARAM;
274 open_square = strchr(cur, '[');
275 if (open_square != NULL) {
276 close_square = strchr(cur, ']');
277 if (close_square == NULL) {
278 rc = LUSTRE_CFG_RC_BAD_PARAM;
282 comma = strchr(cur, ',');
283 if (comma != NULL && comma > close_square) {
285 len = next - close_square;
291 comma = strchr(cur, ',');
301 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist, cur, len);
302 if (rc != LUSTRE_CFG_RC_NO_ERR)
308 return LUSTRE_CFG_RC_NO_ERR;
311 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
313 if (intf_descr->cpt_expr != NULL)
314 cfs_expr_list_free(intf_descr->cpt_expr);
321 int lustre_lnet_config_lib_init(void)
323 return register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH,
324 LNET_DEV_MAJOR, LNET_DEV_MINOR);
327 void lustre_lnet_config_lib_uninit(void)
329 unregister_ioc_dev(LNET_DEV_ID);
332 int lustre_lnet_config_ni_system(bool up, bool load_ni_from_mod,
333 int seq_no, struct cYAML **err_rc)
335 struct libcfs_ioctl_data data;
338 char err_str[LNET_MAX_STR_LEN];
340 snprintf(err_str, sizeof(err_str), "\"Success\"");
342 LIBCFS_IOC_INIT(data);
344 /* Reverse logic is used here in order not to change
345 * the lctl utility */
346 data.ioc_flags = load_ni_from_mod ? 0 : 1;
348 opc = up ? IOC_LIBCFS_CONFIGURE : IOC_LIBCFS_UNCONFIGURE;
350 rc = l_ioctl(LNET_DEV_ID, opc, &data);
354 "\"LNet %s error: %s\"", (up) ? "configure" :
355 "unconfigure", strerror(errno));
359 cYAML_build_error(rc, seq_no, (up) ? CONFIG_CMD : UNCONFIG_CMD,
360 "lnet", err_str, err_rc);
365 static lnet_nid_t *allocate_create_nid_array(char **nids, __u32 num_nids,
368 lnet_nid_t *array = NULL;
372 snprintf(err_str, LNET_MAX_STR_LEN, "no NIDs to add");
376 array = calloc(sizeof(*array) * num_nids, 1);
378 snprintf(err_str, LNET_MAX_STR_LEN, "out of memory");
382 for (i = 0; i < num_nids; i++) {
383 array[i] = libcfs_str2nid(nids[i]);
384 if (array[i] == LNET_NID_ANY) {
386 snprintf(err_str, LNET_MAX_STR_LEN,
396 static int dispatch_peer_ni_cmd(lnet_nid_t knid, lnet_nid_t nid, __u32 cmd,
397 struct lnet_ioctl_peer_cfg *data,
398 char *err_str, char *cmd_str)
402 data->prcfg_key_nid = knid;
403 data->prcfg_cfg_nid = nid;
405 rc = l_ioctl(LNET_DEV_ID, cmd, data);
410 "\"cannot %s peer ni: %s\"",
411 (cmd_str) ? cmd_str : "add", strerror(errno));
417 int lustre_lnet_config_peer_nid(char *knid, char **nid, int num_nids,
418 bool mr, int seq_no, struct cYAML **err_rc)
420 struct lnet_ioctl_peer_cfg data;
421 lnet_nid_t key_nid = LNET_NID_ANY;
422 int rc = LUSTRE_CFG_RC_NO_ERR;
424 bool nid0_used = false;
425 char err_str[LNET_MAX_STR_LEN] = {0};
426 lnet_nid_t *nids = allocate_create_nid_array(nid, num_nids, err_str);
429 key_nid = libcfs_str2nid(knid);
430 if (key_nid == LNET_NID_ANY) {
431 snprintf(err_str, sizeof(err_str),
434 rc = LUSTRE_CFG_RC_MISSING_PARAM;
437 } else if (!nids || nids[0] == LNET_NID_ANY) {
438 snprintf(err_str, sizeof(err_str),
439 "no NIDs provided for configuration");
440 rc = LUSTRE_CFG_RC_MISSING_PARAM;
443 key_nid = LNET_NID_ANY;
446 snprintf(err_str, sizeof(err_str), "\"Success\"");
448 LIBCFS_IOC_INIT_V2(data, prcfg_hdr);
452 * if key_nid is not specified use the first nid in the list of
453 * nids provided as the key_nid. NOTE: on entering 'if' we must
454 * have at least 1 NID
456 if (key_nid == LNET_NID_ANY) {
461 /* Create the key_nid first */
462 rc = dispatch_peer_ni_cmd(key_nid, LNET_NID_ANY,
463 IOC_LIBCFS_ADD_PEER_NI,
464 &data, err_str, "add");
469 /* add the rest of the nids to the key nid if any are available */
470 for (idx = nid0_used ? 1 : 0 ; nids && idx < num_nids; idx++) {
472 * If key_nid is not provided then the first nid in the
473 * list becomes the key_nid. First time round the loop use
474 * LNET_NID_ANY for the first parameter, then use nid[0]
475 * as the key nid after wards
477 rc = dispatch_peer_ni_cmd(key_nid, nids[idx],
478 IOC_LIBCFS_ADD_PEER_NI, &data,
488 cYAML_build_error(rc, seq_no, ADD_CMD, "peer_ni", err_str, err_rc);
492 int lustre_lnet_del_peer_nid(char *knid, char **nid, int num_nids,
493 int seq_no, struct cYAML **err_rc)
495 struct lnet_ioctl_peer_cfg data;
497 int rc = LUSTRE_CFG_RC_NO_ERR;
499 char err_str[LNET_MAX_STR_LEN] = {0};
500 lnet_nid_t *nids = allocate_create_nid_array(nid, num_nids, err_str);
503 snprintf(err_str, sizeof(err_str),
504 "\"Primary nid is not provided\"");
505 rc = LUSTRE_CFG_RC_MISSING_PARAM;
508 key_nid = libcfs_str2nid(knid);
509 if (key_nid == LNET_NID_ANY) {
510 rc = LUSTRE_CFG_RC_BAD_PARAM;
511 snprintf(err_str, sizeof(err_str),
518 snprintf(err_str, sizeof(err_str), "\"Success\"");
520 LIBCFS_IOC_INIT_V2(data, prcfg_hdr);
521 if (!nids || nids[0] == LNET_NID_ANY) {
522 rc = dispatch_peer_ni_cmd(key_nid, LNET_NID_ANY,
523 IOC_LIBCFS_DEL_PEER_NI,
524 &data, err_str, "del");
528 for (idx = 0; nids && idx < num_nids; idx++) {
529 rc = dispatch_peer_ni_cmd(key_nid, nids[idx],
530 IOC_LIBCFS_DEL_PEER_NI, &data,
540 cYAML_build_error(rc, seq_no, DEL_CMD, "peer_ni", err_str, err_rc);
544 int lustre_lnet_config_route(char *nw, char *gw, int hops, int prio,
545 int seq_no, struct cYAML **err_rc)
547 struct lnet_ioctl_config_data data;
548 lnet_nid_t gateway_nid;
549 int rc = LUSTRE_CFG_RC_NO_ERR;
550 __u32 net = LNET_NIDNET(LNET_NID_ANY);
551 char err_str[LNET_MAX_STR_LEN];
553 snprintf(err_str, sizeof(err_str), "\"Success\"");
555 if (nw == NULL || gw == NULL) {
558 "\"missing mandatory parameter(s): '%s'\"",
559 (nw == NULL && gw == NULL) ? "network, gateway" :
560 (nw == NULL) ? "network" : "gateway");
561 rc = LUSTRE_CFG_RC_MISSING_PARAM;
565 net = libcfs_str2net(nw);
566 if (net == LNET_NIDNET(LNET_NID_ANY)) {
569 "\"cannot parse net %s\"", nw);
570 rc = LUSTRE_CFG_RC_BAD_PARAM;
574 if (LNET_NETTYP(net) == CIBLND ||
575 LNET_NETTYP(net) == OPENIBLND ||
576 LNET_NETTYP(net) == IIBLND ||
577 LNET_NETTYP(net) == VIBLND) {
580 "\"obselete LNet type '%s'\"", libcfs_lnd2str(net));
581 rc = LUSTRE_CFG_RC_BAD_PARAM;
585 gateway_nid = libcfs_str2nid(gw);
586 if (gateway_nid == LNET_NID_ANY) {
589 "\"cannot parse gateway NID '%s'\"", gw);
590 rc = LUSTRE_CFG_RC_BAD_PARAM;
595 /* hops is undefined */
596 hops = LNET_UNDEFINED_HOPS;
597 } else if (hops < 1 || hops > 255) {
600 "\"invalid hop count %d, must be between 1 and 255\"",
602 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
608 } else if (prio < 0) {
611 "\"invalid priority %d, must be greater than 0\"",
613 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
617 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
619 data.cfg_config_u.cfg_route.rtr_hop = hops;
620 data.cfg_config_u.cfg_route.rtr_priority = prio;
621 data.cfg_nid = gateway_nid;
623 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
628 "\"cannot add route: %s\"", strerror(errno));
633 cYAML_build_error(rc, seq_no, ADD_CMD, "route", err_str, err_rc);
638 int lustre_lnet_del_route(char *nw, char *gw,
639 int seq_no, struct cYAML **err_rc)
641 struct lnet_ioctl_config_data data;
642 lnet_nid_t gateway_nid;
643 int rc = LUSTRE_CFG_RC_NO_ERR;
644 __u32 net = LNET_NIDNET(LNET_NID_ANY);
645 char err_str[LNET_MAX_STR_LEN];
647 snprintf(err_str, sizeof(err_str), "\"Success\"");
649 if (nw == NULL || gw == NULL) {
652 "\"missing mandatory parameter(s): '%s'\"",
653 (nw == NULL && gw == NULL) ? "network, gateway" :
654 (nw == NULL) ? "network" : "gateway");
655 rc = LUSTRE_CFG_RC_MISSING_PARAM;
659 net = libcfs_str2net(nw);
660 if (net == LNET_NIDNET(LNET_NID_ANY)) {
663 "\"cannot parse net '%s'\"", nw);
664 rc = LUSTRE_CFG_RC_BAD_PARAM;
668 if (LNET_NETTYP(net) == CIBLND ||
669 LNET_NETTYP(net) == OPENIBLND ||
670 LNET_NETTYP(net) == IIBLND ||
671 LNET_NETTYP(net) == VIBLND) {
674 "\"obselete LNet type '%s'\"", libcfs_lnd2str(net));
675 rc = LUSTRE_CFG_RC_BAD_PARAM;
679 gateway_nid = libcfs_str2nid(gw);
680 if (gateway_nid == LNET_NID_ANY) {
683 "\"cannot parse gateway NID '%s'\"", gw);
684 rc = LUSTRE_CFG_RC_BAD_PARAM;
688 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
690 data.cfg_nid = gateway_nid;
692 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
697 "\"cannot delete route: %s\"", strerror(errno));
702 cYAML_build_error(rc, seq_no, DEL_CMD, "route", err_str, err_rc);
707 int lustre_lnet_show_route(char *nw, char *gw, int hops, int prio, int detail,
708 int seq_no, struct cYAML **show_rc,
709 struct cYAML **err_rc)
711 struct lnet_ioctl_config_data data;
712 lnet_nid_t gateway_nid;
713 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
715 __u32 net = LNET_NIDNET(LNET_NID_ANY);
717 struct cYAML *root = NULL, *route = NULL, *item = NULL;
718 struct cYAML *first_seq = NULL;
719 char err_str[LNET_MAX_STR_LEN];
722 snprintf(err_str, sizeof(err_str),
723 "\"out of memory\"");
726 net = libcfs_str2net(nw);
727 if (net == LNET_NIDNET(LNET_NID_ANY)) {
730 "\"cannot parse net '%s'\"", nw);
731 rc = LUSTRE_CFG_RC_BAD_PARAM;
735 if (LNET_NETTYP(net) == CIBLND ||
736 LNET_NETTYP(net) == OPENIBLND ||
737 LNET_NETTYP(net) == IIBLND ||
738 LNET_NETTYP(net) == VIBLND) {
741 "\"obsolete LNet type '%s'\"",
742 libcfs_lnd2str(net));
743 rc = LUSTRE_CFG_RC_BAD_PARAM;
747 /* show all routes without filtering on net */
748 net = LNET_NIDNET(LNET_NID_ANY);
752 gateway_nid = libcfs_str2nid(gw);
753 if (gateway_nid == LNET_NID_ANY) {
756 "\"cannot parse gateway NID '%s'\"", gw);
757 rc = LUSTRE_CFG_RC_BAD_PARAM;
761 /* show all routes with out filtering on gateway */
762 gateway_nid = LNET_NID_ANY;
764 if ((hops < 1 && hops != -1) || hops > 255) {
767 "\"invalid hop count %d, must be between 0 and 256\"",
769 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
773 /* create struct cYAML root object */
774 root = cYAML_create_object(NULL, NULL);
778 route = cYAML_create_seq(root, "route");
783 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
786 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
792 /* filter on provided data */
793 if (net != LNET_NIDNET(LNET_NID_ANY) &&
797 if (gateway_nid != LNET_NID_ANY &&
798 gateway_nid != data.cfg_nid)
802 hops != data.cfg_config_u.cfg_route.rtr_hop)
806 prio != data.cfg_config_u.cfg_route.rtr_priority)
809 /* default rc to -1 incase we hit the goto */
813 item = cYAML_create_seq_item(route);
817 if (first_seq == NULL)
820 if (cYAML_create_string(item, "net",
821 libcfs_net2str(data.cfg_net)) == NULL)
824 if (cYAML_create_string(item, "gateway",
825 libcfs_nid2str(data.cfg_nid)) == NULL)
829 if (cYAML_create_number(item, "hop",
830 data.cfg_config_u.cfg_route.
835 if (cYAML_create_number(item, "priority",
837 cfg_route.rtr_priority) == NULL)
840 if (cYAML_create_string(item, "state",
841 data.cfg_config_u.cfg_route.
843 "up" : "down") == NULL)
848 /* print output iff show_rc is not provided */
850 cYAML_print_tree(root);
852 if (l_errno != ENOENT) {
855 "\"cannot get routes: %s\"",
860 rc = LUSTRE_CFG_RC_NO_ERR;
862 snprintf(err_str, sizeof(err_str), "\"success\"");
864 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
865 cYAML_free_tree(root);
866 } else if (show_rc != NULL && *show_rc != NULL) {
867 struct cYAML *show_node;
868 /* find the route node, if one doesn't exist then
869 * insert one. Otherwise add to the one there
871 show_node = cYAML_get_object_item(*show_rc, "route");
872 if (show_node != NULL && cYAML_is_sequence(show_node)) {
873 cYAML_insert_child(show_node, first_seq);
876 } else if (show_node == NULL) {
877 cYAML_insert_sibling((*show_rc)->cy_child,
881 cYAML_free_tree(root);
887 cYAML_build_error(rc, seq_no, SHOW_CMD, "route", err_str, err_rc);
892 static int socket_intf_query(int request, char *intf,
898 if (strlen(intf) >= IFNAMSIZ || ifr == NULL)
899 return LUSTRE_CFG_RC_BAD_PARAM;
901 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
903 return LUSTRE_CFG_RC_BAD_PARAM;
905 strcpy(ifr->ifr_name, intf);
906 rc = ioctl(sockfd, request, ifr);
908 return LUSTRE_CFG_RC_BAD_PARAM;
914 * for each interface in the array of interfaces find the IP address of
915 * that interface, create its nid and add it to an array of NIDs.
916 * Stop if any of the interfaces is down
918 static int lustre_lnet_intf2nids(struct lnet_dlc_network_descr *nw,
919 lnet_nid_t **nids, __u32 *nnids)
921 int i = 0, count = 0, rc;
924 struct lnet_dlc_intf_descr *intf;
926 if (nw == NULL || nids == NULL)
927 return LUSTRE_CFG_RC_BAD_PARAM;
929 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network)
932 *nids = calloc(count, sizeof(lnet_nid_t));
934 return LUSTRE_CFG_RC_OUT_OF_MEM;
936 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network) {
937 memset(&ifr, 0, sizeof(ifr));
938 rc = socket_intf_query(SIOCGIFFLAGS, intf->intf_name, &ifr);
942 if ((ifr.ifr_flags & IFF_UP) == 0) {
943 rc = LUSTRE_CFG_RC_BAD_PARAM;
947 memset(&ifr, 0, sizeof(ifr));
948 rc = socket_intf_query(SIOCGIFADDR, intf->intf_name, &ifr);
952 ip = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
954 (*nids)[i] = LNET_MKNID(nw->nw_id, ip);
969 * called repeatedly until a match or no more ip range
971 * ip_range expression
972 * interface list with all the interface names.
973 * all the interfaces in the system.
975 * try to match the ip_range expr to one of the interfaces' IPs in
976 * the system. If we hit a patch for an interface. Check if that
977 * interface name is in the list.
979 * If there are more than one interface in the list, then make sure
980 * that the IPs for all of these interfaces match the ip ranges
983 * for each interface in intf_list
984 * look up the intf name in ifa
985 * if not there then no match
986 * check ip obtained from ifa against a match to any of the
988 * If no match, then fail
990 * The result is that all the interfaces have to match.
992 int lustre_lnet_match_ip_to_intf(struct ifaddrs *ifa,
993 struct list_head *intf_list,
994 struct list_head *ip_ranges)
998 struct lnet_dlc_intf_descr *intf_descr;
999 struct ifaddrs *ifaddr = ifa;
1000 struct lustre_lnet_ip_range_descr *ip_range;
1004 * if there are no explicit interfaces, and no ip ranges, then
1005 * configure the first tcp interface we encounter.
1007 if (list_empty(intf_list) && list_empty(ip_ranges)) {
1008 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1009 if (ifaddr->ifa_addr == NULL)
1012 if ((ifaddr->ifa_flags & IFF_UP) == 0)
1015 family = ifaddr->ifa_addr->sa_family;
1016 if (family == AF_INET) {
1017 rc = lustre_lnet_add_intf_descr
1018 (intf_list, ifaddr->ifa_name,
1019 strlen(ifaddr->ifa_name));
1021 if (rc != LUSTRE_CFG_RC_NO_ERR)
1024 return LUSTRE_CFG_RC_MATCH;
1027 return LUSTRE_CFG_RC_NO_MATCH;
1031 * First interface which matches an IP pattern will be used
1033 if (list_empty(intf_list)) {
1035 * no interfaces provided in the rule, but an ip range is
1036 * provided, so try and match an interface to the ip
1039 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1040 if (ifaddr->ifa_addr == NULL)
1043 if ((ifaddr->ifa_flags & IFF_UP) == 0)
1046 family = ifaddr->ifa_addr->sa_family;
1047 if (family == AF_INET) {
1048 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->
1051 list_for_each_entry(ip_range, ip_ranges,
1053 rc = cfs_ip_addr_match(bswap_32(ip),
1054 &ip_range->ipr_expr);
1058 rc = lustre_lnet_add_intf_descr
1059 (intf_list, ifaddr->ifa_name,
1060 strlen(ifaddr->ifa_name));
1062 if (rc != LUSTRE_CFG_RC_NO_ERR)
1065 return LUSTRE_CFG_RC_MATCH;
1072 * If an interface is explicitly specified the ip-range might or
1073 * might not be specified. if specified the interface needs to match the
1074 * ip-range. If no ip-range then the interfaces are
1075 * automatically matched if they are all up.
1076 * If > 1 interfaces all the interfaces must match for the NI to
1079 list_for_each_entry(intf_descr, intf_list, intf_on_network) {
1080 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1081 if (ifaddr->ifa_addr == NULL)
1084 family = ifaddr->ifa_addr->sa_family;
1085 if (family == AF_INET &&
1086 strcmp(intf_descr->intf_name,
1087 ifaddr->ifa_name) == 0)
1092 return LUSTRE_CFG_RC_NO_MATCH;
1094 if ((ifaddr->ifa_flags & IFF_UP) == 0)
1095 return LUSTRE_CFG_RC_NO_MATCH;
1097 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr.s_addr;
1099 list_for_each_entry(ip_range, ip_ranges, ipr_entry) {
1100 rc = cfs_ip_addr_match(bswap_32(ip), &ip_range->ipr_expr);
1104 return LUSTRE_CFG_RC_NO_MATCH;
1108 return LUSTRE_CFG_RC_MATCH;
1111 int lustre_lnet_resolve_ip2nets_rule(struct lustre_lnet_ip2nets *ip2nets,
1112 lnet_nid_t **nids, __u32 *nnids)
1114 struct ifaddrs *ifa;
1115 int rc = LUSTRE_CFG_RC_NO_ERR;
1117 rc = getifaddrs(&ifa);
1121 rc = lustre_lnet_match_ip_to_intf(ifa,
1122 &ip2nets->ip2nets_net.nw_intflist,
1123 &ip2nets->ip2nets_ip_ranges);
1124 if (rc != LUSTRE_CFG_RC_MATCH) {
1129 rc = lustre_lnet_intf2nids(&ip2nets->ip2nets_net, nids, nnids);
1130 if (rc != LUSTRE_CFG_RC_NO_ERR) {
1141 lustre_lnet_ioctl_config_ni(struct list_head *intf_list,
1142 struct lnet_ioctl_config_lnd_tunables *tunables,
1143 struct cfs_expr_list *global_cpts,
1144 lnet_nid_t *nids, char *err_str)
1147 struct lnet_ioctl_config_ni *conf;
1148 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1149 int rc = LUSTRE_CFG_RC_NO_ERR, i = 0;
1152 struct lnet_dlc_intf_descr *intf_descr;
1154 struct cfs_expr_list *cpt_expr;
1156 list_for_each_entry(intf_descr, intf_list,
1158 if (i == 0 && tunables != NULL)
1159 len = sizeof(struct lnet_ioctl_config_ni) +
1160 sizeof(struct lnet_ioctl_config_lnd_tunables);
1162 len = sizeof(struct lnet_ioctl_config_ni);
1164 data = calloc(1, len);
1165 conf = (struct lnet_ioctl_config_ni*) data;
1166 if (i == 0 && tunables != NULL)
1167 tun = (struct lnet_ioctl_config_lnd_tunables*)
1170 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1171 conf->lic_cfg_hdr.ioc_len = len;
1172 conf->lic_nid = nids[i];
1173 strncpy(conf->lic_ni_intf[0], intf_descr->intf_name,
1176 if (intf_descr->cpt_expr != NULL)
1177 cpt_expr = intf_descr->cpt_expr;
1178 else if (global_cpts != NULL)
1179 cpt_expr = global_cpts;
1183 if (cpt_expr != NULL) {
1184 count = cfs_expr_list_values(cpt_expr,
1185 LNET_MAX_SHOW_NUM_CPT,
1188 memcpy(conf->lic_cpts, cpt_array,
1189 sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1198 conf->lic_ncpts = count;
1200 if (i == 0 && tunables != NULL)
1201 /* TODO put in the LND tunables */
1202 memcpy(tun, tunables, sizeof(*tunables));
1204 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1209 "\"cannot add network: %s\"", strerror(errno));
1215 return LUSTRE_CFG_RC_NO_ERR;
1219 lustre_lnet_config_ip2nets(struct lustre_lnet_ip2nets *ip2nets,
1220 struct lnet_ioctl_config_lnd_tunables *tunables,
1221 struct cfs_expr_list *global_cpts,
1222 int seq_no, struct cYAML **err_rc)
1224 lnet_nid_t *nids = NULL;
1227 char err_str[LNET_MAX_STR_LEN];
1229 snprintf(err_str, sizeof(err_str), "\"success\"");
1231 if (ip2nets == NULL ||
1232 list_empty(&ip2nets->ip2nets_net.nw_intflist)) {
1235 "\"incomplete ip2nets information\"");
1236 rc = LUSTRE_CFG_RC_BAD_PARAM;
1240 rc = lustre_lnet_resolve_ip2nets_rule(ip2nets, &nids, &nnids);
1241 if (rc != LUSTRE_CFG_RC_NO_ERR && rc != LUSTRE_CFG_RC_MATCH) {
1244 "\"cannot resolve ip2nets rule\"");
1248 rc = lustre_lnet_ioctl_config_ni(&ip2nets->ip2nets_net.nw_intflist,
1249 tunables, global_cpts, nids,
1251 if (rc != LUSTRE_CFG_RC_NO_ERR)
1255 cYAML_build_error(rc, seq_no, ADD_CMD, "ip2nets", err_str, err_rc);
1259 int lustre_lnet_config_ni(struct lnet_dlc_network_descr *nw_descr,
1260 struct cfs_expr_list *global_cpts,
1262 struct lnet_ioctl_config_lnd_tunables *tunables,
1263 int seq_no, struct cYAML **err_rc)
1266 struct lnet_ioctl_config_ni *conf;
1267 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1268 char buf[LNET_MAX_STR_LEN];
1269 int rc = LUSTRE_CFG_RC_NO_ERR;
1270 char err_str[LNET_MAX_STR_LEN];
1271 lnet_nid_t *nids = NULL;
1275 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1278 snprintf(err_str, sizeof(err_str), "\"success\"");
1280 if (ip2net == NULL && nw_descr == NULL) {
1283 "\"mandatory parameters not specified.\"");
1284 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1288 if (ip2net != NULL && strlen(ip2net) >= sizeof(buf)) {
1291 "\"ip2net string too long %d\"",
1292 (int)strlen(ip2net));
1293 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1297 if (ip2net != NULL) {
1298 if (tunables != NULL)
1299 len = sizeof(struct lnet_ioctl_config_ni) +
1300 sizeof(struct lnet_ioctl_config_lnd_tunables);
1302 len = sizeof(struct lnet_ioctl_config_ni);
1303 data = calloc(1, len);
1304 conf = (struct lnet_ioctl_config_ni*) data;
1305 if (tunables != NULL)
1306 tun = (struct lnet_ioctl_config_lnd_tunables*)
1307 (data + sizeof(*conf));
1309 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1310 conf->lic_cfg_hdr.ioc_len = len;
1311 strncpy(conf->lic_legacy_ip2nets, ip2net,
1314 if (global_cpts != NULL) {
1315 count = cfs_expr_list_values(global_cpts,
1316 LNET_MAX_SHOW_NUM_CPT,
1319 memcpy(conf->lic_cpts, cpt_array,
1320 sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1329 conf->lic_ncpts = count;
1331 if (tunables != NULL)
1332 memcpy(tun, tunables, sizeof(*tunables));
1334 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1339 "\"cannot add network: %s\"", strerror(errno));
1346 if (LNET_NETTYP(nw_descr->nw_id) == LOLND)
1347 return LUSTRE_CFG_RC_NO_ERR;
1349 if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1352 "\"cannot parse net '%s'\"",
1353 libcfs_net2str(nw_descr->nw_id));
1354 rc = LUSTRE_CFG_RC_BAD_PARAM;
1358 if (list_empty(&nw_descr->nw_intflist)) {
1361 "\"no interface name provided\"");
1362 rc = LUSTRE_CFG_RC_BAD_PARAM;
1366 rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids);
1368 snprintf(err_str, sizeof(err_str),
1369 "\"bad parameter\"");
1370 rc = LUSTRE_CFG_RC_BAD_PARAM;
1374 rc = lustre_lnet_ioctl_config_ni(&nw_descr->nw_intflist,
1375 tunables, global_cpts, nids,
1379 if (nw_descr != NULL) {
1380 list_for_each_entry_safe(intf_descr, tmp,
1381 &nw_descr->nw_intflist,
1383 if (intf_descr->cpt_expr != NULL)
1384 cfs_expr_list_free(intf_descr->cpt_expr);
1389 cYAML_build_error(rc, seq_no, ADD_CMD, "net", err_str, err_rc);
1400 int lustre_lnet_del_ni(struct lnet_dlc_network_descr *nw_descr,
1401 int seq_no, struct cYAML **err_rc)
1403 struct lnet_ioctl_config_ni data;
1404 int rc = LUSTRE_CFG_RC_NO_ERR, i;
1405 char err_str[LNET_MAX_STR_LEN];
1406 lnet_nid_t *nids = NULL;
1408 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1410 if (LNET_NETTYP(nw_descr->nw_id) == LOLND)
1411 return LUSTRE_CFG_RC_NO_ERR;
1413 snprintf(err_str, sizeof(err_str), "\"success\"");
1415 if (nw_descr == NULL) {
1418 "\"missing mandatory parameter\"");
1419 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1423 if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1426 "\"cannot parse net '%s'\"",
1427 libcfs_net2str(nw_descr->nw_id));
1428 rc = LUSTRE_CFG_RC_BAD_PARAM;
1432 rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids);
1434 snprintf(err_str, sizeof(err_str),
1435 "\"bad parameter\"");
1436 rc = LUSTRE_CFG_RC_BAD_PARAM;
1441 * no interfaces just the nw_id is specified
1444 nids = calloc(1, sizeof(*nids));
1446 snprintf(err_str, sizeof(err_str),
1447 "\"out of memory\"");
1448 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1451 nids[0] = LNET_MKNID(nw_descr->nw_id, 0);
1455 for (i = 0; i < nnids; i++) {
1456 LIBCFS_IOC_INIT_V2(data, lic_cfg_hdr);
1457 data.lic_nid = nids[i];
1459 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_LOCAL_NI, &data);
1464 "\"cannot del network: %s\"", strerror(errno));
1468 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
1470 if (intf_descr->cpt_expr != NULL)
1471 cfs_expr_list_free(intf_descr->cpt_expr);
1476 cYAML_build_error(rc, seq_no, DEL_CMD, "net", err_str, err_rc);
1484 int lustre_lnet_show_net(char *nw, int detail, int seq_no,
1485 struct cYAML **show_rc, struct cYAML **err_rc)
1488 struct lnet_ioctl_config_ni *ni_data;
1489 struct lnet_ioctl_config_lnd_tunables *lnd;
1490 struct lnet_ioctl_element_stats *stats;
1491 __u32 net = LNET_NIDNET(LNET_NID_ANY);
1492 __u32 prev_net = LNET_NIDNET(LNET_NID_ANY);
1493 int rc = LUSTRE_CFG_RC_OUT_OF_MEM, i, j;
1495 struct cYAML *root = NULL, *tunables = NULL,
1496 *net_node = NULL, *interfaces = NULL,
1497 *item = NULL, *first_seq = NULL,
1498 *tmp = NULL, *statistics = NULL;
1499 int str_buf_len = LNET_MAX_SHOW_NUM_CPT * 2;
1500 char str_buf[str_buf_len];
1502 char err_str[LNET_MAX_STR_LEN];
1503 bool exist = false, new_net = true;
1505 size_t buf_size = sizeof(*ni_data) + sizeof(*lnd) + sizeof(*stats);
1507 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1509 buf = calloc(1, buf_size);
1513 ni_data = (struct lnet_ioctl_config_ni *)buf;
1516 net = libcfs_str2net(nw);
1517 if (net == LNET_NIDNET(LNET_NID_ANY)) {
1520 "\"cannot parse net '%s'\"", nw);
1521 rc = LUSTRE_CFG_RC_BAD_PARAM;
1526 root = cYAML_create_object(NULL, NULL);
1530 net_node = cYAML_create_seq(root, "net");
1531 if (net_node == NULL)
1538 memset(buf, 0, buf_size);
1540 LIBCFS_IOC_INIT_V2(*ni_data, lic_cfg_hdr);
1542 * set the ioc_len to the proper value since INIT assumes
1545 ni_data->lic_cfg_hdr.ioc_len = buf_size;
1546 ni_data->lic_idx = i;
1548 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LOCAL_NI, ni_data);
1554 rc_net = LNET_NIDNET(ni_data->lic_nid);
1556 /* filter on provided data */
1557 if (net != LNET_NIDNET(LNET_NID_ANY) &&
1561 /* default rc to -1 in case we hit the goto */
1565 stats = (struct lnet_ioctl_element_stats *)ni_data->lic_bulk;
1566 lnd = (struct lnet_ioctl_config_lnd_tunables *)
1567 (ni_data->lic_bulk + sizeof(*stats));
1569 if (rc_net != prev_net) {
1576 if (!cYAML_create_string(net_node, "net type",
1577 libcfs_net2str(rc_net)))
1580 tmp = cYAML_create_seq(net_node, "local NI(s)");
1586 /* create the tree to be printed. */
1587 item = cYAML_create_seq_item(tmp);
1591 if (first_seq == NULL)
1594 if (cYAML_create_string(item, "nid",
1595 libcfs_nid2str(ni_data->lic_nid)) == NULL)
1598 if (cYAML_create_string(item,
1600 (ni_data->lic_status ==
1601 LNET_NI_STATUS_UP) ?
1602 "up" : "down") == NULL)
1605 /* don't add interfaces unless there is at least one
1607 if (strlen(ni_data->lic_ni_intf[0]) > 0) {
1608 interfaces = cYAML_create_object(item, "interfaces");
1609 if (interfaces == NULL)
1612 for (j = 0; j < LNET_MAX_INTERFACES; j++) {
1613 if (strlen(ni_data->lic_ni_intf[j]) > 0) {
1615 sizeof(str_buf), "%d", j);
1616 if (cYAML_create_string(interfaces,
1618 ni_data->lic_ni_intf[j]) ==
1628 statistics = cYAML_create_object(item, "statistics");
1629 if (statistics == NULL)
1632 if (cYAML_create_number(statistics, "send_count",
1637 if (cYAML_create_number(statistics, "recv_count",
1642 if (cYAML_create_number(statistics, "drop_count",
1647 tunables = cYAML_create_object(item, "tunables");
1651 rc = lustre_net_show_tunables(tunables, &lnd->lt_cmn);
1652 if (rc != LUSTRE_CFG_RC_NO_ERR)
1655 tunables = cYAML_create_object(item, "lnd tunables");
1656 if (tunables == NULL)
1659 rc = lustre_ni_show_tunables(tunables, LNET_NETTYP(rc_net),
1661 if (rc != LUSTRE_CFG_RC_NO_ERR)
1664 if (cYAML_create_number(item, "tcp bonding",
1665 ni_data->lic_tcp_bonding)
1669 if (cYAML_create_number(item, "dev cpt",
1670 ni_data->lic_dev_cpt) == NULL)
1673 /* out put the CPTs in the format: "[x,x,x,...]" */
1674 limit = str_buf + str_buf_len - 3;
1675 pos += snprintf(pos, limit - pos, "\"[");
1676 for (j = 0 ; ni_data->lic_ncpts >= 1 &&
1677 j < ni_data->lic_ncpts &&
1679 pos += snprintf(pos, limit - pos,
1680 "%d", ni_data->lic_cpts[j]);
1681 if ((j + 1) < ni_data->lic_ncpts)
1682 pos += snprintf(pos, limit - pos, ",");
1684 pos += snprintf(pos, 3, "]\"");
1686 if (ni_data->lic_ncpts >= 1 &&
1687 cYAML_create_string(item, "CPT",
1693 /* Print out the net information only if show_rc is not provided */
1694 if (show_rc == NULL)
1695 cYAML_print_tree(root);
1697 if (l_errno != ENOENT) {
1700 "\"cannot get networks: %s\"",
1705 rc = LUSTRE_CFG_RC_NO_ERR;
1707 snprintf(err_str, sizeof(err_str), "\"success\"");
1709 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
1710 cYAML_free_tree(root);
1711 } else if (show_rc != NULL && *show_rc != NULL) {
1712 struct cYAML *show_node;
1713 /* find the net node, if one doesn't exist
1714 * then insert one. Otherwise add to the one there
1716 show_node = cYAML_get_object_item(*show_rc, "net");
1717 if (show_node != NULL && cYAML_is_sequence(show_node)) {
1718 cYAML_insert_child(show_node, first_seq);
1721 } else if (show_node == NULL) {
1722 cYAML_insert_sibling((*show_rc)->cy_child,
1726 cYAML_free_tree(root);
1732 cYAML_build_error(rc, seq_no, SHOW_CMD, "net", err_str, err_rc);
1737 int lustre_lnet_enable_routing(int enable, int seq_no, struct cYAML **err_rc)
1739 struct lnet_ioctl_config_data data;
1740 int rc = LUSTRE_CFG_RC_NO_ERR;
1741 char err_str[LNET_MAX_STR_LEN];
1743 snprintf(err_str, sizeof(err_str), "\"success\"");
1745 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1746 data.cfg_config_u.cfg_buffers.buf_enable = (enable) ? 1 : 0;
1748 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIG_RTR, &data);
1753 "\"cannot %s routing %s\"",
1754 (enable) ? "enable" : "disable", strerror(errno));
1759 cYAML_build_error(rc, seq_no,
1760 (enable) ? ADD_CMD : DEL_CMD,
1761 "routing", err_str, err_rc);
1766 int lustre_lnet_config_numa_range(int range, int seq_no, struct cYAML **err_rc)
1768 struct lnet_ioctl_numa_range data;
1769 int rc = LUSTRE_CFG_RC_NO_ERR;
1770 char err_str[LNET_MAX_STR_LEN];
1772 snprintf(err_str, sizeof(err_str), "\"success\"");
1777 "\"range must be >= 0\"");
1778 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1782 LIBCFS_IOC_INIT_V2(data, nr_hdr);
1783 data.nr_range = range;
1785 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_SET_NUMA_RANGE, &data);
1790 "\"cannot configure buffers: %s\"", strerror(errno));
1795 cYAML_build_error(rc, seq_no, ADD_CMD, "numa_range", err_str, err_rc);
1800 int lustre_lnet_config_buffers(int tiny, int small, int large, int seq_no,
1801 struct cYAML **err_rc)
1803 struct lnet_ioctl_config_data data;
1804 int rc = LUSTRE_CFG_RC_NO_ERR;
1805 char err_str[LNET_MAX_STR_LEN];
1807 snprintf(err_str, sizeof(err_str), "\"success\"");
1809 /* -1 indicates to ignore changes to this field */
1810 if (tiny < -1 || small < -1 || large < -1) {
1813 "\"tiny, small and large must be >= 0\"");
1814 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1818 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1819 data.cfg_config_u.cfg_buffers.buf_tiny = tiny;
1820 data.cfg_config_u.cfg_buffers.buf_small = small;
1821 data.cfg_config_u.cfg_buffers.buf_large = large;
1823 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_BUF, &data);
1828 "\"cannot configure buffers: %s\"", strerror(errno));
1833 cYAML_build_error(rc, seq_no, ADD_CMD, "buf", err_str, err_rc);
1838 int lustre_lnet_show_routing(int seq_no, struct cYAML **show_rc,
1839 struct cYAML **err_rc)
1841 struct lnet_ioctl_config_data *data;
1842 struct lnet_ioctl_pool_cfg *pool_cfg = NULL;
1843 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1846 char *pools[LNET_NRBPOOLS] = {"tiny", "small", "large"};
1847 int buf_count[LNET_NRBPOOLS] = {0};
1848 struct cYAML *root = NULL, *pools_node = NULL,
1849 *type_node = NULL, *item = NULL, *cpt = NULL,
1850 *first_seq = NULL, *buffers = NULL;
1852 char err_str[LNET_MAX_STR_LEN];
1853 char node_name[LNET_MAX_STR_LEN];
1856 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1858 buf = calloc(1, sizeof(*data) + sizeof(*pool_cfg));
1862 data = (struct lnet_ioctl_config_data *)buf;
1864 root = cYAML_create_object(NULL, NULL);
1868 pools_node = cYAML_create_seq(root, "routing");
1869 if (pools_node == NULL)
1873 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
1874 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
1875 sizeof(struct lnet_ioctl_pool_cfg);
1876 data->cfg_count = i;
1878 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_BUF, data);
1886 pool_cfg = (struct lnet_ioctl_pool_cfg *)data->cfg_bulk;
1888 snprintf(node_name, sizeof(node_name), "cpt[%d]", i);
1889 item = cYAML_create_seq_item(pools_node);
1893 if (first_seq == NULL)
1896 cpt = cYAML_create_object(item, node_name);
1900 /* create the tree and print */
1901 for (j = 0; j < LNET_NRBPOOLS; j++) {
1902 type_node = cYAML_create_object(cpt, pools[j]);
1903 if (type_node == NULL)
1905 if (cYAML_create_number(type_node, "npages",
1906 pool_cfg->pl_pools[j].pl_npages)
1909 if (cYAML_create_number(type_node, "nbuffers",
1910 pool_cfg->pl_pools[j].
1911 pl_nbuffers) == NULL)
1913 if (cYAML_create_number(type_node, "credits",
1914 pool_cfg->pl_pools[j].
1915 pl_credits) == NULL)
1917 if (cYAML_create_number(type_node, "mincredits",
1918 pool_cfg->pl_pools[j].
1919 pl_mincredits) == NULL)
1921 /* keep track of the total count for each of the
1922 * tiny, small and large buffers */
1923 buf_count[j] += pool_cfg->pl_pools[j].pl_nbuffers;
1927 if (pool_cfg != NULL) {
1928 item = cYAML_create_seq_item(pools_node);
1932 if (cYAML_create_number(item, "enable", pool_cfg->pl_routing) ==
1937 /* create a buffers entry in the show. This is necessary so that
1938 * if the YAML output is used to configure a node, the buffer
1939 * configuration takes hold */
1940 buffers = cYAML_create_object(root, "buffers");
1941 if (buffers == NULL)
1944 for (i = 0; i < LNET_NRBPOOLS; i++) {
1945 if (cYAML_create_number(buffers, pools[i], buf_count[i]) == NULL)
1949 if (show_rc == NULL)
1950 cYAML_print_tree(root);
1952 if (l_errno != ENOENT) {
1955 "\"cannot get routing information: %s\"",
1960 rc = LUSTRE_CFG_RC_NO_ERR;
1962 snprintf(err_str, sizeof(err_str), "\"success\"");
1963 rc = LUSTRE_CFG_RC_NO_ERR;
1967 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
1968 cYAML_free_tree(root);
1969 } else if (show_rc != NULL && *show_rc != NULL) {
1970 struct cYAML *routing_node;
1971 /* there should exist only one routing block and one
1972 * buffers block. If there already exists a previous one
1973 * then don't add another */
1974 routing_node = cYAML_get_object_item(*show_rc, "routing");
1975 if (routing_node == NULL) {
1976 cYAML_insert_sibling((*show_rc)->cy_child,
1980 cYAML_free_tree(root);
1986 cYAML_build_error(rc, seq_no, SHOW_CMD, "routing", err_str, err_rc);
1991 int lustre_lnet_show_peer(char *knid, int detail, int seq_no,
1992 struct cYAML **show_rc, struct cYAML **err_rc)
1994 struct lnet_ioctl_peer_cfg *peer_info;
1995 struct lnet_peer_ni_credit_info *lpni_cri;
1996 struct lnet_ioctl_element_stats *lpni_stats;
1997 int rc = LUSTRE_CFG_RC_OUT_OF_MEM, ncpt = 0, i = 0, j = 0;
1999 struct cYAML *root = NULL, *peer = NULL, *peer_ni = NULL,
2000 *first_seq = NULL, *peer_root = NULL, *tmp = NULL;
2001 char err_str[LNET_MAX_STR_LEN];
2002 lnet_nid_t prev_primary_nid = LNET_NID_ANY, primary_nid = LNET_NID_ANY;
2003 int data_size = sizeof(*peer_info) + sizeof(*lpni_cri) +
2004 sizeof(*lpni_stats);
2005 char *data = calloc(data_size, 1);
2006 bool new_peer = true;
2008 snprintf(err_str, sizeof(err_str),
2009 "\"out of memory\"");
2014 peer_info = (struct lnet_ioctl_peer_cfg *)data;
2016 /* create struct cYAML root object */
2017 root = cYAML_create_object(NULL, NULL);
2021 peer_root = cYAML_create_seq(root, "peer");
2022 if (peer_root == NULL)
2026 primary_nid = libcfs_str2nid(knid);
2030 memset(data, 0, data_size);
2031 LIBCFS_IOC_INIT_V2(*peer_info, prcfg_hdr);
2032 peer_info->prcfg_hdr.ioc_len = data_size;
2033 peer_info->prcfg_idx = i;
2035 rc = l_ioctl(LNET_DEV_ID,
2036 IOC_LIBCFS_GET_PEER_NI, peer_info);
2042 if (primary_nid != LNET_NID_ANY &&
2043 primary_nid != peer_info->prcfg_key_nid)
2046 lpni_cri = (struct lnet_peer_ni_credit_info*)peer_info->prcfg_bulk;
2047 lpni_stats = (struct lnet_ioctl_element_stats *)
2048 (peer_info->prcfg_bulk +
2051 peer = cYAML_create_seq_item(peer_root);
2055 if (peer_info->prcfg_key_nid != prev_primary_nid) {
2056 prev_primary_nid = peer_info->prcfg_key_nid;
2061 lnet_nid_t pnid = peer_info->prcfg_key_nid;
2062 if (cYAML_create_string(peer, "primary nid",
2063 libcfs_nid2str(pnid))
2066 if (cYAML_create_string(peer, "Multi-Rail",
2067 peer_info->prcfg_mr ?
2071 tmp = cYAML_create_seq(peer, "peer ni");
2077 if (first_seq == NULL)
2080 peer_ni = cYAML_create_seq_item(tmp);
2081 if (peer_ni == NULL)
2084 if (cYAML_create_string(peer_ni, "nid",
2086 (peer_info->prcfg_cfg_nid))
2090 if (cYAML_create_string(peer_ni, "state",
2091 lpni_cri->cr_aliveness)
2098 if (cYAML_create_number(peer_ni, "max_ni_tx_credits",
2099 lpni_cri->cr_ni_peer_tx_credits)
2103 if (cYAML_create_number(peer_ni, "available_tx_credits",
2104 lpni_cri->cr_peer_tx_credits)
2108 if (cYAML_create_number(peer_ni, "min_tx_credits",
2109 lpni_cri->cr_peer_min_tx_credits)
2113 if (cYAML_create_number(peer_ni, "tx_q_num_of_buf",
2114 lpni_cri->cr_peer_tx_qnob)
2118 if (cYAML_create_number(peer_ni, "available_rtr_credits",
2119 lpni_cri->cr_peer_rtr_credits)
2123 if (cYAML_create_number(peer_ni, "min_rtr_credits",
2124 lpni_cri->cr_peer_min_rtr_credits)
2128 if (cYAML_create_number(peer_ni, "send_count",
2129 lpni_stats->send_count)
2133 if (cYAML_create_number(peer_ni, "recv_count",
2134 lpni_stats->recv_count)
2138 if (cYAML_create_number(peer_ni, "drop_count",
2139 lpni_stats->drop_count)
2143 if (cYAML_create_number(peer_ni, "refcount",
2144 lpni_cri->cr_refcount) == NULL)
2148 if (l_errno != ENOENT) {
2151 "\"cannot get peer information: %s\"",
2160 /* print output iff show_rc is not provided */
2161 if (show_rc == NULL)
2162 cYAML_print_tree(root);
2164 snprintf(err_str, sizeof(err_str), "\"success\"");
2165 rc = LUSTRE_CFG_RC_NO_ERR;
2168 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2169 cYAML_free_tree(root);
2170 } else if (show_rc != NULL && *show_rc != NULL) {
2171 struct cYAML *show_node;
2172 /* find the peer node, if one doesn't exist then
2173 * insert one. Otherwise add to the one there
2175 show_node = cYAML_get_object_item(*show_rc,
2177 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2178 cYAML_insert_child(show_node, first_seq);
2181 } else if (show_node == NULL) {
2182 cYAML_insert_sibling((*show_rc)->cy_child,
2186 cYAML_free_tree(root);
2192 cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
2198 int lustre_lnet_show_numa_range(int seq_no, struct cYAML **show_rc,
2199 struct cYAML **err_rc)
2201 struct lnet_ioctl_numa_range data;
2202 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2204 char err_str[LNET_MAX_STR_LEN];
2205 struct cYAML *root = NULL, *range = NULL;
2207 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2209 LIBCFS_IOC_INIT_V2(data, nr_hdr);
2211 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NUMA_RANGE, &data);
2216 "\"cannot get numa range: %s\"",
2222 root = cYAML_create_object(NULL, NULL);
2226 range = cYAML_create_object(root, "numa");
2230 if (cYAML_create_number(range, "range",
2231 data.nr_range) == NULL)
2234 if (show_rc == NULL)
2235 cYAML_print_tree(root);
2237 snprintf(err_str, sizeof(err_str), "\"success\"");
2239 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2240 cYAML_free_tree(root);
2241 } else if (show_rc != NULL && *show_rc != NULL) {
2242 cYAML_insert_sibling((*show_rc)->cy_child,
2249 cYAML_build_error(rc, seq_no, SHOW_CMD, "numa", err_str, err_rc);
2254 int lustre_lnet_show_stats(int seq_no, struct cYAML **show_rc,
2255 struct cYAML **err_rc)
2257 struct lnet_ioctl_lnet_stats data;
2258 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2260 char err_str[LNET_MAX_STR_LEN];
2261 struct cYAML *root = NULL, *stats = NULL;
2263 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2265 LIBCFS_IOC_INIT_V2(data, st_hdr);
2267 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LNET_STATS, &data);
2272 "\"cannot get lnet statistics: %s\"",
2278 root = cYAML_create_object(NULL, NULL);
2282 stats = cYAML_create_object(root, "statistics");
2286 if (cYAML_create_number(stats, "msgs_alloc",
2287 data.st_cntrs.msgs_alloc) == NULL)
2290 if (cYAML_create_number(stats, "msgs_max",
2291 data.st_cntrs.msgs_max) == NULL)
2294 if (cYAML_create_number(stats, "errors",
2295 data.st_cntrs.errors) == NULL)
2298 if (cYAML_create_number(stats, "send_count",
2299 data.st_cntrs.send_count) == NULL)
2302 if (cYAML_create_number(stats, "recv_count",
2303 data.st_cntrs.recv_count) == NULL)
2306 if (cYAML_create_number(stats, "route_count",
2307 data.st_cntrs.route_count) == NULL)
2310 if (cYAML_create_number(stats, "drop_count",
2311 data.st_cntrs.drop_count) == NULL)
2314 if (cYAML_create_number(stats, "send_length",
2315 data.st_cntrs.send_length) == NULL)
2318 if (cYAML_create_number(stats, "recv_length",
2319 data.st_cntrs.recv_length) == NULL)
2322 if (cYAML_create_number(stats, "route_length",
2323 data.st_cntrs.route_length) == NULL)
2326 if (cYAML_create_number(stats, "drop_length",
2327 data.st_cntrs.drop_length) == NULL)
2330 if (show_rc == NULL)
2331 cYAML_print_tree(root);
2333 snprintf(err_str, sizeof(err_str), "\"success\"");
2335 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2336 cYAML_free_tree(root);
2337 } else if (show_rc != NULL && *show_rc != NULL) {
2338 cYAML_insert_sibling((*show_rc)->cy_child,
2345 cYAML_build_error(rc, seq_no, SHOW_CMD, "statistics", err_str, err_rc);
2350 typedef int (*cmd_handler_t)(struct cYAML *tree,
2351 struct cYAML **show_rc,
2352 struct cYAML **err_rc);
2354 static int handle_yaml_config_route(struct cYAML *tree, struct cYAML **show_rc,
2355 struct cYAML **err_rc)
2357 struct cYAML *net, *gw, *hop, *prio, *seq_no;
2359 net = cYAML_get_object_item(tree, "net");
2360 gw = cYAML_get_object_item(tree, "gateway");
2361 hop = cYAML_get_object_item(tree, "hop");
2362 prio = cYAML_get_object_item(tree, "priority");
2363 seq_no = cYAML_get_object_item(tree, "seq_no");
2365 return lustre_lnet_config_route((net) ? net->cy_valuestring : NULL,
2366 (gw) ? gw->cy_valuestring : NULL,
2367 (hop) ? hop->cy_valueint : -1,
2368 (prio) ? prio->cy_valueint : -1,
2369 (seq_no) ? seq_no->cy_valueint : -1,
2373 static void yaml_free_string_array(char **array, int num)
2376 char **sub_array = array;
2378 for (i = 0; i < num; i++) {
2379 if (*sub_array != NULL)
2389 * 0: <intf_name>['['<expr>']']
2390 * 1: <intf_name>['['<expr>']']
2392 static int yaml_copy_intf_info(struct cYAML *intf_tree,
2393 struct lnet_dlc_network_descr *nw_descr)
2395 struct cYAML *child = NULL;
2396 int intf_num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
2397 struct lnet_dlc_intf_descr *intf_descr, *tmp;
2399 if (intf_tree == NULL || nw_descr == NULL)
2400 return LUSTRE_CFG_RC_BAD_PARAM;
2402 /* now grab all the interfaces and their cpts */
2403 child = intf_tree->cy_child;
2404 while (child != NULL) {
2405 if (child->cy_valuestring == NULL) {
2406 child = child->cy_next;
2410 if (strlen(child->cy_valuestring) >= LNET_MAX_STR_LEN)
2413 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist,
2414 child->cy_valuestring,
2415 strlen(child->cy_valuestring));
2416 if (rc != LUSTRE_CFG_RC_NO_ERR)
2420 child = child->cy_next;
2424 return LUSTRE_CFG_RC_MISSING_PARAM;
2429 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
2431 if (intf_descr->cpt_expr != NULL)
2432 cfs_expr_list_free(intf_descr->cpt_expr);
2440 yaml_extract_cmn_tunables(struct cYAML *tree,
2441 struct lnet_ioctl_config_lnd_cmn_tunables *tunables,
2442 struct cfs_expr_list **global_cpts)
2444 struct cYAML *tun, *item, *smp;
2447 tun = cYAML_get_object_item(tree, "tunables");
2449 item = cYAML_get_object_item(tun, "peer_timeout");
2451 tunables->lct_peer_timeout = item->cy_valueint;
2452 item = cYAML_get_object_item(tun, "peer_credits");
2454 tunables->lct_peer_tx_credits = item->cy_valueint;
2455 item = cYAML_get_object_item(tun, "peer_buffer_credits");
2457 tunables->lct_peer_rtr_credits = item->cy_valueint;
2458 item = cYAML_get_object_item(tun, "credits");
2460 tunables->lct_max_tx_credits = item->cy_valueint;
2461 smp = cYAML_get_object_item(tun, "CPT");
2463 rc = cfs_expr_list_parse(smp->cy_valuestring,
2464 strlen(smp->cy_valuestring),
2465 0, UINT_MAX, global_cpts);
2467 *global_cpts = NULL;
2477 yaml_extract_tunables(struct cYAML *tree,
2478 struct lnet_ioctl_config_lnd_tunables *tunables,
2479 struct cfs_expr_list **global_cpts,
2484 rc = yaml_extract_cmn_tunables(tree, &tunables->lt_cmn,
2490 lustre_yaml_extract_lnd_tunables(tree, net_type,
2498 * - net type: <net>[<NUM>]
2500 * - nid: <ip>@<net>[<NUM>]
2503 * 0: <intf_name>['['<expr>']']
2504 * 1: <intf_name>['['<expr>']']
2506 * peer_timeout: <NUM>
2507 * peer_credits: <NUM>
2508 * peer_buffer_credits: <NUM>
2511 * peercredits_hiw: <NUM>
2512 * map_on_demand: <NUM>
2513 * concurrent_sends: <NUM>
2514 * fmr_pool_size: <NUM>
2515 * fmr_flush_trigger: <NUM>
2518 * At least one interface is required. If no interfaces are provided the
2519 * network interface can not be configured.
2521 static int handle_yaml_config_ni(struct cYAML *tree, struct cYAML **show_rc,
2522 struct cYAML **err_rc)
2524 struct cYAML *net, *intf, *seq_no, *ip2net = NULL, *local_nis = NULL,
2526 int num_entries = 0, rc;
2527 struct lnet_dlc_network_descr nw_descr;
2528 struct cfs_expr_list *global_cpts = NULL;
2529 struct lnet_ioctl_config_lnd_tunables tunables;
2532 memset(&tunables, 0, sizeof(tunables));
2534 INIT_LIST_HEAD(&nw_descr.network_on_rule);
2535 INIT_LIST_HEAD(&nw_descr.nw_intflist);
2537 ip2net = cYAML_get_object_item(tree, "ip2net");
2538 net = cYAML_get_object_item(tree, "net type");
2540 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
2543 * if neither net nor ip2nets are present, then we can not
2544 * configure the network.
2546 if (!net && !ip2net)
2547 return LUSTRE_CFG_RC_MISSING_PARAM;
2549 local_nis = cYAML_get_object_item(tree, "local NI(s)");
2550 if (local_nis == NULL)
2551 return LUSTRE_CFG_RC_MISSING_PARAM;
2553 if (!cYAML_is_sequence(local_nis))
2554 return LUSTRE_CFG_RC_BAD_PARAM;
2556 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
2557 intf = cYAML_get_object_item(item, "interfaces");
2560 num_entries = yaml_copy_intf_info(intf, &nw_descr);
2561 if (num_entries <= 0) {
2562 cYAML_build_error(num_entries, -1, "ni", "add",
2563 "bad interface list",
2565 return LUSTRE_CFG_RC_BAD_PARAM;
2569 found = yaml_extract_tunables(tree, &tunables, &global_cpts,
2570 LNET_NETTYP(nw_descr.nw_id));
2571 seq_no = cYAML_get_object_item(tree, "seq_no");
2573 rc = lustre_lnet_config_ni(&nw_descr,
2575 (ip2net) ? ip2net->cy_valuestring : NULL,
2576 (found) ? &tunables: NULL,
2577 (seq_no) ? seq_no->cy_valueint : -1,
2580 if (global_cpts != NULL)
2581 cfs_expr_list_free(global_cpts);
2588 * - net-spec: <tcp|o2ib|gni>[NUM]
2590 * 0: <intf name>['['<expr>']']
2591 * 1: <intf name>['['<expr>']']
2593 * 0: <expr.expr.expr.expr>
2594 * 1: <expr.expr.expr.expr>
2596 static int handle_yaml_config_ip2nets(struct cYAML *tree,
2597 struct cYAML **show_rc,
2598 struct cYAML **err_rc)
2600 struct cYAML *net, *ip_range, *item = NULL, *intf = NULL,
2602 struct lustre_lnet_ip2nets ip2nets;
2603 struct lustre_lnet_ip_range_descr *ip_range_descr = NULL,
2605 int rc = LUSTRE_CFG_RC_NO_ERR;
2606 struct cfs_expr_list *global_cpts = NULL;
2607 struct cfs_expr_list *el, *el_tmp;
2608 struct lnet_ioctl_config_lnd_tunables tunables;
2609 struct lnet_dlc_intf_descr *intf_descr, *intf_tmp;
2612 memset(&tunables, 0, sizeof(tunables));
2614 /* initialize all lists */
2615 INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
2616 INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
2617 INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
2619 net = cYAML_get_object_item(tree, "net-spec");
2621 return LUSTRE_CFG_RC_BAD_PARAM;
2623 if (net != NULL && net->cy_valuestring == NULL)
2624 return LUSTRE_CFG_RC_BAD_PARAM;
2626 /* assign the network id */
2627 ip2nets.ip2nets_net.nw_id = libcfs_str2net(net->cy_valuestring);
2628 if (ip2nets.ip2nets_net.nw_id == LNET_NID_ANY)
2629 return LUSTRE_CFG_RC_BAD_PARAM;
2631 seq_no = cYAML_get_object_item(tree, "seq_no");
2633 intf = cYAML_get_object_item(tree, "interfaces");
2635 rc = yaml_copy_intf_info(intf, &ip2nets.ip2nets_net);
2637 return LUSTRE_CFG_RC_BAD_PARAM;
2640 ip_range = cYAML_get_object_item(tree, "ip-range");
2641 if (ip_range != NULL) {
2642 item = ip_range->cy_child;
2643 while (item != NULL) {
2644 if (item->cy_valuestring == NULL) {
2645 item = item->cy_next;
2649 rc = lustre_lnet_add_ip_range(&ip2nets.ip2nets_ip_ranges,
2650 item->cy_valuestring);
2652 if (rc != LUSTRE_CFG_RC_NO_ERR)
2655 item = item->cy_next;
2659 found = yaml_extract_tunables(tree, &tunables, &global_cpts,
2660 LNET_NETTYP(ip2nets.ip2nets_net.nw_id));
2662 rc = lustre_lnet_config_ip2nets(&ip2nets,
2663 (found) ? &tunables : NULL,
2665 (seq_no) ? seq_no->cy_valueint : -1,
2669 * don't stop because there was no match. Continue processing the
2670 * rest of the rules. If non-match then nothing is configured
2672 if (rc == LUSTRE_CFG_RC_NO_MATCH)
2673 rc = LUSTRE_CFG_RC_NO_ERR;
2675 list_for_each_entry_safe(intf_descr, intf_tmp,
2676 &ip2nets.ip2nets_net.nw_intflist,
2678 if (intf_descr->cpt_expr != NULL)
2679 cfs_expr_list_free(intf_descr->cpt_expr);
2683 list_for_each_entry_safe(ip_range_descr, tmp,
2684 &ip2nets.ip2nets_ip_ranges,
2686 list_for_each_entry_safe(el, el_tmp, &ip_range_descr->ipr_expr,
2688 cfs_expr_list_free(el);
2689 free(ip_range_descr);
2695 static int handle_yaml_del_ni(struct cYAML *tree, struct cYAML **show_rc,
2696 struct cYAML **err_rc)
2698 struct cYAML *net = NULL, *intf = NULL, *seq_no = NULL, *item = NULL,
2700 int num_entries, rc;
2701 struct lnet_dlc_network_descr nw_descr;
2703 INIT_LIST_HEAD(&nw_descr.network_on_rule);
2704 INIT_LIST_HEAD(&nw_descr.nw_intflist);
2706 net = cYAML_get_object_item(tree, "net type");
2708 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
2710 local_nis = cYAML_get_object_item(tree, "local NI(s)");
2711 if (local_nis == NULL)
2712 return LUSTRE_CFG_RC_MISSING_PARAM;
2714 if (!cYAML_is_sequence(local_nis))
2715 return LUSTRE_CFG_RC_BAD_PARAM;
2717 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
2718 intf = cYAML_get_object_item(item, "interfaces");
2721 num_entries = yaml_copy_intf_info(intf, &nw_descr);
2722 if (num_entries <= 0) {
2723 cYAML_build_error(num_entries, -1, "ni", "add",
2724 "bad interface list",
2726 return LUSTRE_CFG_RC_BAD_PARAM;
2730 seq_no = cYAML_get_object_item(tree, "seq_no");
2732 rc = lustre_lnet_del_ni((net) ? &nw_descr : NULL,
2733 (seq_no) ? seq_no->cy_valueint : -1,
2739 static int yaml_copy_peer_nids(struct cYAML *tree, char ***nidsppp)
2741 struct cYAML *nids_entry = NULL, *child = NULL, *entry = NULL;
2743 int num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
2745 nids_entry = cYAML_get_object_item(tree, "peer ni");
2746 if (cYAML_is_sequence(nids_entry)) {
2747 while (cYAML_get_next_seq_item(nids_entry, &child))
2752 return LUSTRE_CFG_RC_MISSING_PARAM;
2754 nids = calloc(sizeof(*nids) * num, 1);
2756 return LUSTRE_CFG_RC_OUT_OF_MEM;
2758 /* now grab all the nids */
2761 while (cYAML_get_next_seq_item(nids_entry, &child)) {
2762 entry = cYAML_get_object_item(child, "nid");
2765 nids[num] = calloc(strlen(entry->cy_valuestring) + 1, 1);
2767 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2770 strncpy(nids[num], entry->cy_valuestring,
2771 strlen(entry->cy_valuestring));
2781 yaml_free_string_array(nids, num);
2786 static int handle_yaml_config_peer(struct cYAML *tree, struct cYAML **show_rc,
2787 struct cYAML **err_rc)
2791 struct cYAML *seq_no, *key_nid, *non_mr;
2793 num = yaml_copy_peer_nids(tree, &nids);
2797 seq_no = cYAML_get_object_item(tree, "seq_no");
2798 key_nid = cYAML_get_object_item(tree, "primary nid");
2799 non_mr = cYAML_get_object_item(tree, "non_mr");
2801 rc = lustre_lnet_config_peer_nid((key_nid) ? key_nid->cy_valuestring : NULL,
2803 (non_mr) ? false : true,
2804 (seq_no) ? seq_no->cy_valueint : -1,
2807 yaml_free_string_array(nids, num);
2811 static int handle_yaml_del_peer(struct cYAML *tree, struct cYAML **show_rc,
2812 struct cYAML **err_rc)
2816 struct cYAML *seq_no, *key_nid;
2818 num = yaml_copy_peer_nids(tree, &nids);
2822 seq_no = cYAML_get_object_item(tree, "seq_no");
2823 key_nid = cYAML_get_object_item(tree, "primary nid");
2825 rc = lustre_lnet_del_peer_nid((key_nid) ? key_nid->cy_valuestring : NULL,
2827 (seq_no) ? seq_no->cy_valueint : -1,
2830 yaml_free_string_array(nids, num);
2834 static int handle_yaml_config_buffers(struct cYAML *tree,
2835 struct cYAML **show_rc,
2836 struct cYAML **err_rc)
2839 struct cYAML *tiny, *small, *large, *seq_no;
2841 tiny = cYAML_get_object_item(tree, "tiny");
2842 small = cYAML_get_object_item(tree, "small");
2843 large = cYAML_get_object_item(tree, "large");
2844 seq_no = cYAML_get_object_item(tree, "seq_no");
2846 rc = lustre_lnet_config_buffers((tiny) ? tiny->cy_valueint : -1,
2847 (small) ? small->cy_valueint : -1,
2848 (large) ? large->cy_valueint : -1,
2849 (seq_no) ? seq_no->cy_valueint : -1,
2855 static int handle_yaml_config_routing(struct cYAML *tree,
2856 struct cYAML **show_rc,
2857 struct cYAML **err_rc)
2859 int rc = LUSTRE_CFG_RC_NO_ERR;
2860 struct cYAML *seq_no, *enable;
2862 seq_no = cYAML_get_object_item(tree, "seq_no");
2863 enable = cYAML_get_object_item(tree, "enable");
2866 rc = lustre_lnet_enable_routing(enable->cy_valueint,
2868 seq_no->cy_valueint : -1,
2875 static int handle_yaml_del_route(struct cYAML *tree, struct cYAML **show_rc,
2876 struct cYAML **err_rc)
2880 struct cYAML *seq_no;
2882 net = cYAML_get_object_item(tree, "net");
2883 gw = cYAML_get_object_item(tree, "gateway");
2884 seq_no = cYAML_get_object_item(tree, "seq_no");
2886 return lustre_lnet_del_route((net) ? net->cy_valuestring : NULL,
2887 (gw) ? gw->cy_valuestring : NULL,
2888 (seq_no) ? seq_no->cy_valueint : -1,
2892 static int handle_yaml_del_routing(struct cYAML *tree, struct cYAML **show_rc,
2893 struct cYAML **err_rc)
2895 struct cYAML *seq_no;
2897 seq_no = cYAML_get_object_item(tree, "seq_no");
2899 return lustre_lnet_enable_routing(0, (seq_no) ?
2900 seq_no->cy_valueint : -1,
2904 static int handle_yaml_show_route(struct cYAML *tree, struct cYAML **show_rc,
2905 struct cYAML **err_rc)
2911 struct cYAML *detail;
2912 struct cYAML *seq_no;
2914 net = cYAML_get_object_item(tree, "net");
2915 gw = cYAML_get_object_item(tree, "gateway");
2916 hop = cYAML_get_object_item(tree, "hop");
2917 prio = cYAML_get_object_item(tree, "priority");
2918 detail = cYAML_get_object_item(tree, "detail");
2919 seq_no = cYAML_get_object_item(tree, "seq_no");
2921 return lustre_lnet_show_route((net) ? net->cy_valuestring : NULL,
2922 (gw) ? gw->cy_valuestring : NULL,
2923 (hop) ? hop->cy_valueint : -1,
2924 (prio) ? prio->cy_valueint : -1,
2925 (detail) ? detail->cy_valueint : 0,
2926 (seq_no) ? seq_no->cy_valueint : -1,
2931 static int handle_yaml_show_net(struct cYAML *tree, struct cYAML **show_rc,
2932 struct cYAML **err_rc)
2934 struct cYAML *net, *detail, *seq_no;
2936 net = cYAML_get_object_item(tree, "net");
2937 detail = cYAML_get_object_item(tree, "detail");
2938 seq_no = cYAML_get_object_item(tree, "seq_no");
2940 return lustre_lnet_show_net((net) ? net->cy_valuestring : NULL,
2941 (detail) ? detail->cy_valueint : 0,
2942 (seq_no) ? seq_no->cy_valueint : -1,
2947 static int handle_yaml_show_routing(struct cYAML *tree, struct cYAML **show_rc,
2948 struct cYAML **err_rc)
2950 struct cYAML *seq_no;
2952 seq_no = cYAML_get_object_item(tree, "seq_no");
2954 return lustre_lnet_show_routing((seq_no) ? seq_no->cy_valueint : -1,
2958 static int handle_yaml_show_credits(struct cYAML *tree, struct cYAML **show_rc,
2959 struct cYAML **err_rc)
2961 struct cYAML *seq_no, *key_nid, *detail;
2963 seq_no = cYAML_get_object_item(tree, "seq_no");
2964 detail = cYAML_get_object_item(tree, "detail");
2965 key_nid = cYAML_get_object_item(tree, "key_nid");
2967 return lustre_lnet_show_peer((key_nid) ? key_nid->cy_valuestring : NULL,
2968 (detail) ? detail->cy_valueint : 0,
2969 (seq_no) ? seq_no->cy_valueint : -1,
2973 static int handle_yaml_show_stats(struct cYAML *tree, struct cYAML **show_rc,
2974 struct cYAML **err_rc)
2976 struct cYAML *seq_no;
2978 seq_no = cYAML_get_object_item(tree, "seq_no");
2980 return lustre_lnet_show_stats((seq_no) ? seq_no->cy_valueint : -1,
2984 static int handle_yaml_config_numa(struct cYAML *tree, struct cYAML **show_rc,
2985 struct cYAML **err_rc)
2987 struct cYAML *seq_no, *range;
2989 seq_no = cYAML_get_object_item(tree, "seq_no");
2990 range = cYAML_get_object_item(tree, "range");
2992 return lustre_lnet_config_numa_range(range ? range->cy_valueint : -1,
2993 seq_no ? seq_no->cy_valueint : -1,
2997 static int handle_yaml_del_numa(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_config_numa_range(0, seq_no ? seq_no->cy_valueint : -1,
3008 static int handle_yaml_show_numa(struct cYAML *tree, struct cYAML **show_rc,
3009 struct cYAML **err_rc)
3011 struct cYAML *seq_no;
3013 seq_no = cYAML_get_object_item(tree, "seq_no");
3015 return lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint : -1,
3019 struct lookup_cmd_hdlr_tbl {
3024 static struct lookup_cmd_hdlr_tbl lookup_config_tbl[] = {
3025 {"route", handle_yaml_config_route},
3026 {"net", handle_yaml_config_ni},
3027 {"ip2nets", handle_yaml_config_ip2nets},
3028 {"peer", handle_yaml_config_peer},
3029 {"routing", handle_yaml_config_routing},
3030 {"buffers", handle_yaml_config_buffers},
3031 {"numa", handle_yaml_config_numa},
3035 static struct lookup_cmd_hdlr_tbl lookup_del_tbl[] = {
3036 {"route", handle_yaml_del_route},
3037 {"net", handle_yaml_del_ni},
3038 {"peer", handle_yaml_del_peer},
3039 {"routing", handle_yaml_del_routing},
3040 {"numa", handle_yaml_del_numa},
3044 static struct lookup_cmd_hdlr_tbl lookup_show_tbl[] = {
3045 {"route", handle_yaml_show_route},
3046 {"net", handle_yaml_show_net},
3047 {"buffers", handle_yaml_show_routing},
3048 {"routing", handle_yaml_show_routing},
3049 {"credits", handle_yaml_show_credits},
3050 {"statistics", handle_yaml_show_stats},
3051 {"numa", handle_yaml_show_numa},
3055 static cmd_handler_t lookup_fn(char *key,
3056 struct lookup_cmd_hdlr_tbl *tbl)
3062 for (i = 0; tbl[i].name != NULL; i++) {
3063 if (strncmp(key, tbl[i].name, strlen(tbl[i].name)) == 0)
3070 static int lustre_yaml_cb_helper(char *f, struct lookup_cmd_hdlr_tbl *table,
3071 struct cYAML **show_rc, struct cYAML **err_rc)
3073 struct cYAML *tree, *item = NULL, *head, *child;
3075 char err_str[LNET_MAX_STR_LEN];
3076 int rc = LUSTRE_CFG_RC_NO_ERR, return_rc = LUSTRE_CFG_RC_NO_ERR;
3078 tree = cYAML_build_tree(f, NULL, 0, err_rc, false);
3080 return LUSTRE_CFG_RC_BAD_PARAM;
3082 child = tree->cy_child;
3083 while (child != NULL) {
3084 cb = lookup_fn(child->cy_string, table);
3086 snprintf(err_str, sizeof(err_str),
3087 "\"call back for '%s' not found\"",
3089 cYAML_build_error(LUSTRE_CFG_RC_BAD_PARAM, -1,
3090 "yaml", "helper", err_str, err_rc);
3094 if (cYAML_is_sequence(child)) {
3095 while ((head = cYAML_get_next_seq_item(child, &item))
3097 rc = cb(head, show_rc, err_rc);
3098 if (rc != LUSTRE_CFG_RC_NO_ERR)
3102 rc = cb(child, show_rc, err_rc);
3103 if (rc != LUSTRE_CFG_RC_NO_ERR)
3107 child = child->cy_next;
3111 cYAML_free_tree(tree);
3116 int lustre_yaml_config(char *f, struct cYAML **err_rc)
3118 return lustre_yaml_cb_helper(f, lookup_config_tbl,
3122 int lustre_yaml_del(char *f, struct cYAML **err_rc)
3124 return lustre_yaml_cb_helper(f, lookup_del_tbl,
3128 int lustre_yaml_show(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
3130 return lustre_yaml_cb_helper(f, lookup_show_tbl,
3134 int lustre_lnet_send_dbg_task(enum lnet_dbg_task dbg_task,
3135 struct lnet_dbg_task_info *dbg_info,
3136 struct cYAML **show_rc,
3137 struct cYAML **err_rc)
3139 struct lnet_ioctl_dbg *dbg;
3140 struct lnet_dbg_task_info *info;
3141 int rc = LUSTRE_CFG_RC_NO_ERR;
3142 char err_str[LNET_MAX_STR_LEN];
3144 snprintf(err_str, sizeof(err_str), "\"success\"");
3146 dbg = calloc(1, sizeof(*dbg) + sizeof(*info));
3148 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3149 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3153 info = (struct lnet_dbg_task_info *)dbg->dbg_bulk;
3155 LIBCFS_IOC_INIT_V2(*dbg, dbg_hdr);
3157 dbg->dbg_task = dbg_task;
3159 memcpy(info, dbg_info, sizeof(*info));
3161 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DBG, dbg);
3166 "\"debug task failed %s\"", strerror(errno));
3171 cYAML_build_error(rc, -1, DBG_CMD,
3172 "debug", err_str, err_rc);