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);
174 * <intf>[<expr>], <intf>[<expr>],..
176 int lustre_lnet_parse_interfaces(char *intf_str,
177 struct lnet_dlc_network_descr *nw_descr)
182 char *cur = intf_str, *next = NULL;
183 char *end = intf_str + strlen(intf_str);
185 struct lnet_dlc_intf_descr *intf_descr, *tmp;
187 if (nw_descr == NULL)
188 return LUSTRE_CFG_RC_BAD_PARAM;
191 open_square = strchr(cur, '[');
192 if (open_square != NULL) {
193 close_square = strchr(cur, ']');
194 if (close_square == NULL) {
195 rc = LUSTRE_CFG_RC_BAD_PARAM;
199 comma = strchr(cur, ',');
200 if (comma != NULL && comma > close_square) {
202 len = next - close_square;
208 comma = strchr(cur, ',');
218 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist, cur, len);
219 if (rc != LUSTRE_CFG_RC_NO_ERR)
225 return LUSTRE_CFG_RC_NO_ERR;
228 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
230 if (intf_descr->cpt_expr != NULL)
231 cfs_expr_list_free(intf_descr->cpt_expr);
238 int lustre_lnet_config_lib_init(void)
240 return register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH,
241 LNET_DEV_MAJOR, LNET_DEV_MINOR);
244 void lustre_lnet_config_lib_uninit(void)
246 unregister_ioc_dev(LNET_DEV_ID);
249 int lustre_lnet_config_ni_system(bool up, bool load_ni_from_mod,
250 int seq_no, struct cYAML **err_rc)
252 struct libcfs_ioctl_data data;
255 char err_str[LNET_MAX_STR_LEN];
257 snprintf(err_str, sizeof(err_str), "\"Success\"");
259 LIBCFS_IOC_INIT(data);
261 /* Reverse logic is used here in order not to change
262 * the lctl utility */
263 data.ioc_flags = load_ni_from_mod ? 0 : 1;
265 opc = up ? IOC_LIBCFS_CONFIGURE : IOC_LIBCFS_UNCONFIGURE;
267 rc = l_ioctl(LNET_DEV_ID, opc, &data);
271 "\"LNet %s error: %s\"", (up) ? "configure" :
272 "unconfigure", strerror(errno));
276 cYAML_build_error(rc, seq_no, (up) ? CONFIG_CMD : UNCONFIG_CMD,
277 "lnet", err_str, err_rc);
282 static lnet_nid_t *allocate_create_nid_array(char **nids, char *err_str)
284 lnet_nid_t *array = NULL;
288 snprintf(err_str, LNET_MAX_STR_LEN, "no NIDs to add");
292 /* count the size of the array */
293 while (nids[idx] != NULL)
296 array = calloc(sizeof(*array) * idx + 1, 1);
298 snprintf(err_str, LNET_MAX_STR_LEN, "out of memory");
303 while (nids[idx] != NULL) {
304 array[idx] = libcfs_str2nid(nids[idx]);
305 if (array[idx] == LNET_NID_ANY) {
307 snprintf(err_str, LNET_MAX_STR_LEN,
315 /* identify last entry */
316 array[idx] = LNET_NID_ANY;
321 static int dispatch_peer_ni_cmd(lnet_nid_t knid, lnet_nid_t nid, __u32 cmd,
322 struct lnet_ioctl_peer_cfg *data,
323 char *err_str, char *cmd_str)
327 data->prcfg_key_nid = knid;
328 data->prcfg_cfg_nid = nid;
330 rc = l_ioctl(LNET_DEV_ID, cmd, data);
335 "\"cannot %s peer ni: %s\"",
336 (cmd_str) ? cmd_str : "add", strerror(errno));
342 int lustre_lnet_config_peer_nid(char *knid, char **nid, int seq_no,
343 struct cYAML **err_rc)
345 struct lnet_ioctl_peer_cfg data;
346 lnet_nid_t key_nid = LNET_NID_ANY;
347 int rc = LUSTRE_CFG_RC_NO_ERR;
349 char err_str[LNET_MAX_STR_LEN] = {0};
350 lnet_nid_t *nids = allocate_create_nid_array(nid, err_str);
353 key_nid = libcfs_str2nid(knid);
354 if (key_nid == LNET_NID_ANY) {
355 snprintf(err_str, sizeof(err_str),
358 rc = LUSTRE_CFG_RC_MISSING_PARAM;
361 } else if (nids[0] == LNET_NID_ANY) {
362 snprintf(err_str, sizeof(err_str),
363 "no NIDs provided for configuration");
364 rc = LUSTRE_CFG_RC_MISSING_PARAM;
367 key_nid = LNET_NID_ANY;
370 snprintf(err_str, sizeof(err_str), "\"Success\"");
372 LIBCFS_IOC_INIT_V2(data, prcfg_hdr);
373 if (nids[0] == LNET_NID_ANY) {
374 rc = dispatch_peer_ni_cmd(LNET_NID_ANY, key_nid,
375 IOC_LIBCFS_ADD_PEER_NI,
376 &data, err_str, "add");
380 while (nids[idx] != LNET_NID_ANY) {
382 * If key_nid is not provided then the first nid in the
383 * list becomes the key_nid. First time round the loop use
384 * LNET_NID_ANY for the first parameter, then use nid[0]
385 * as the key nid after wards
387 rc = dispatch_peer_ni_cmd(key_nid, nids[idx],
388 IOC_LIBCFS_ADD_PEER_NI, &data,
394 if (idx == 0 && key_nid == LNET_NID_ANY)
403 cYAML_build_error(rc, seq_no, ADD_CMD, "peer_ni", err_str, err_rc);
407 int lustre_lnet_del_peer_nid(char *knid, char **nid, int seq_no,
408 struct cYAML **err_rc)
410 struct lnet_ioctl_peer_cfg data;
412 int rc = LUSTRE_CFG_RC_NO_ERR;
414 char err_str[LNET_MAX_STR_LEN] = {0};
415 lnet_nid_t *nids = allocate_create_nid_array(nid, err_str);
418 snprintf(err_str, sizeof(err_str),
419 "\"Primary nid is not provided\"");
420 rc = LUSTRE_CFG_RC_MISSING_PARAM;
423 key_nid = libcfs_str2nid(knid);
424 if (key_nid == LNET_NID_ANY) {
425 rc = LUSTRE_CFG_RC_BAD_PARAM;
426 snprintf(err_str, sizeof(err_str),
433 snprintf(err_str, sizeof(err_str), "\"Success\"");
435 LIBCFS_IOC_INIT_V2(data, prcfg_hdr);
436 if (nids[0] == LNET_NID_ANY) {
437 rc = dispatch_peer_ni_cmd(key_nid, LNET_NID_ANY,
438 IOC_LIBCFS_DEL_PEER_NI,
439 &data, err_str, "del");
443 while (nids[idx] != LNET_NID_ANY) {
444 rc = dispatch_peer_ni_cmd(key_nid, nids[idx],
445 IOC_LIBCFS_DEL_PEER_NI, &data,
457 cYAML_build_error(rc, seq_no, DEL_CMD, "peer_ni", err_str, err_rc);
461 int lustre_lnet_config_route(char *nw, char *gw, int hops, int prio,
462 int seq_no, struct cYAML **err_rc)
464 struct lnet_ioctl_config_data data;
465 lnet_nid_t gateway_nid;
466 int rc = LUSTRE_CFG_RC_NO_ERR;
467 __u32 net = LNET_NIDNET(LNET_NID_ANY);
468 char err_str[LNET_MAX_STR_LEN];
470 snprintf(err_str, sizeof(err_str), "\"Success\"");
472 if (nw == NULL || gw == NULL) {
475 "\"missing mandatory parameter(s): '%s'\"",
476 (nw == NULL && gw == NULL) ? "network, gateway" :
477 (nw == NULL) ? "network" : "gateway");
478 rc = LUSTRE_CFG_RC_MISSING_PARAM;
482 net = libcfs_str2net(nw);
483 if (net == LNET_NIDNET(LNET_NID_ANY)) {
486 "\"cannot parse net %s\"", nw);
487 rc = LUSTRE_CFG_RC_BAD_PARAM;
491 if (LNET_NETTYP(net) == CIBLND ||
492 LNET_NETTYP(net) == OPENIBLND ||
493 LNET_NETTYP(net) == IIBLND ||
494 LNET_NETTYP(net) == VIBLND) {
497 "\"obselete LNet type '%s'\"", libcfs_lnd2str(net));
498 rc = LUSTRE_CFG_RC_BAD_PARAM;
502 gateway_nid = libcfs_str2nid(gw);
503 if (gateway_nid == LNET_NID_ANY) {
506 "\"cannot parse gateway NID '%s'\"", gw);
507 rc = LUSTRE_CFG_RC_BAD_PARAM;
512 /* hops is undefined */
513 hops = LNET_UNDEFINED_HOPS;
514 } else if (hops < 1 || hops > 255) {
517 "\"invalid hop count %d, must be between 1 and 255\"",
519 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
525 } else if (prio < 0) {
528 "\"invalid priority %d, must be greater than 0\"",
530 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
534 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
536 data.cfg_config_u.cfg_route.rtr_hop = hops;
537 data.cfg_config_u.cfg_route.rtr_priority = prio;
538 data.cfg_nid = gateway_nid;
540 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
545 "\"cannot add route: %s\"", strerror(errno));
550 cYAML_build_error(rc, seq_no, ADD_CMD, "route", err_str, err_rc);
555 int lustre_lnet_del_route(char *nw, char *gw,
556 int seq_no, struct cYAML **err_rc)
558 struct lnet_ioctl_config_data data;
559 lnet_nid_t gateway_nid;
560 int rc = LUSTRE_CFG_RC_NO_ERR;
561 __u32 net = LNET_NIDNET(LNET_NID_ANY);
562 char err_str[LNET_MAX_STR_LEN];
564 snprintf(err_str, sizeof(err_str), "\"Success\"");
566 if (nw == NULL || gw == NULL) {
569 "\"missing mandatory parameter(s): '%s'\"",
570 (nw == NULL && gw == NULL) ? "network, gateway" :
571 (nw == NULL) ? "network" : "gateway");
572 rc = LUSTRE_CFG_RC_MISSING_PARAM;
576 net = libcfs_str2net(nw);
577 if (net == LNET_NIDNET(LNET_NID_ANY)) {
580 "\"cannot parse net '%s'\"", nw);
581 rc = LUSTRE_CFG_RC_BAD_PARAM;
585 if (LNET_NETTYP(net) == CIBLND ||
586 LNET_NETTYP(net) == OPENIBLND ||
587 LNET_NETTYP(net) == IIBLND ||
588 LNET_NETTYP(net) == VIBLND) {
591 "\"obselete LNet type '%s'\"", libcfs_lnd2str(net));
592 rc = LUSTRE_CFG_RC_BAD_PARAM;
596 gateway_nid = libcfs_str2nid(gw);
597 if (gateway_nid == LNET_NID_ANY) {
600 "\"cannot parse gateway NID '%s'\"", gw);
601 rc = LUSTRE_CFG_RC_BAD_PARAM;
605 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
607 data.cfg_nid = gateway_nid;
609 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
614 "\"cannot delete route: %s\"", strerror(errno));
619 cYAML_build_error(rc, seq_no, DEL_CMD, "route", err_str, err_rc);
624 int lustre_lnet_show_route(char *nw, char *gw, int hops, int prio, int detail,
625 int seq_no, struct cYAML **show_rc,
626 struct cYAML **err_rc)
628 struct lnet_ioctl_config_data data;
629 lnet_nid_t gateway_nid;
630 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
632 __u32 net = LNET_NIDNET(LNET_NID_ANY);
634 struct cYAML *root = NULL, *route = NULL, *item = NULL;
635 struct cYAML *first_seq = NULL;
636 char err_str[LNET_MAX_STR_LEN];
639 snprintf(err_str, sizeof(err_str),
640 "\"out of memory\"");
643 net = libcfs_str2net(nw);
644 if (net == LNET_NIDNET(LNET_NID_ANY)) {
647 "\"cannot parse net '%s'\"", nw);
648 rc = LUSTRE_CFG_RC_BAD_PARAM;
652 if (LNET_NETTYP(net) == CIBLND ||
653 LNET_NETTYP(net) == OPENIBLND ||
654 LNET_NETTYP(net) == IIBLND ||
655 LNET_NETTYP(net) == VIBLND) {
658 "\"obsolete LNet type '%s'\"",
659 libcfs_lnd2str(net));
660 rc = LUSTRE_CFG_RC_BAD_PARAM;
664 /* show all routes without filtering on net */
665 net = LNET_NIDNET(LNET_NID_ANY);
669 gateway_nid = libcfs_str2nid(gw);
670 if (gateway_nid == LNET_NID_ANY) {
673 "\"cannot parse gateway NID '%s'\"", gw);
674 rc = LUSTRE_CFG_RC_BAD_PARAM;
678 /* show all routes with out filtering on gateway */
679 gateway_nid = LNET_NID_ANY;
681 if ((hops < 1 && hops != -1) || hops > 255) {
684 "\"invalid hop count %d, must be between 0 and 256\"",
686 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
690 /* create struct cYAML root object */
691 root = cYAML_create_object(NULL, NULL);
695 route = cYAML_create_seq(root, "route");
700 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
703 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
709 /* filter on provided data */
710 if (net != LNET_NIDNET(LNET_NID_ANY) &&
714 if (gateway_nid != LNET_NID_ANY &&
715 gateway_nid != data.cfg_nid)
719 hops != data.cfg_config_u.cfg_route.rtr_hop)
723 prio != data.cfg_config_u.cfg_route.rtr_priority)
726 /* default rc to -1 incase we hit the goto */
730 item = cYAML_create_seq_item(route);
734 if (first_seq == NULL)
737 if (cYAML_create_string(item, "net",
738 libcfs_net2str(data.cfg_net)) == NULL)
741 if (cYAML_create_string(item, "gateway",
742 libcfs_nid2str(data.cfg_nid)) == NULL)
746 if (cYAML_create_number(item, "hop",
747 data.cfg_config_u.cfg_route.
752 if (cYAML_create_number(item, "priority",
754 cfg_route.rtr_priority) == NULL)
757 if (cYAML_create_string(item, "state",
758 data.cfg_config_u.cfg_route.
760 "up" : "down") == NULL)
765 /* print output iff show_rc is not provided */
767 cYAML_print_tree(root);
769 if (l_errno != ENOENT) {
772 "\"cannot get routes: %s\"",
777 rc = LUSTRE_CFG_RC_NO_ERR;
779 snprintf(err_str, sizeof(err_str), "\"success\"");
781 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
782 cYAML_free_tree(root);
783 } else if (show_rc != NULL && *show_rc != NULL) {
784 struct cYAML *show_node;
785 /* find the route node, if one doesn't exist then
786 * insert one. Otherwise add to the one there
788 show_node = cYAML_get_object_item(*show_rc, "route");
789 if (show_node != NULL && cYAML_is_sequence(show_node)) {
790 cYAML_insert_child(show_node, first_seq);
793 } else if (show_node == NULL) {
794 cYAML_insert_sibling((*show_rc)->cy_child,
798 cYAML_free_tree(root);
804 cYAML_build_error(rc, seq_no, SHOW_CMD, "route", err_str, err_rc);
809 static int socket_intf_query(int request, char *intf,
815 if (strlen(intf) >= IFNAMSIZ || ifr == NULL)
816 return LUSTRE_CFG_RC_BAD_PARAM;
818 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
820 return LUSTRE_CFG_RC_BAD_PARAM;
822 strcpy(ifr->ifr_name, intf);
823 rc = ioctl(sockfd, request, ifr);
825 return LUSTRE_CFG_RC_BAD_PARAM;
831 * for each interface in the array of interfaces find the IP address of
832 * that interface, create its nid and add it to an array of NIDs.
833 * Stop if any of the interfaces is down
835 static int lustre_lnet_intf2nids(struct lnet_dlc_network_descr *nw,
836 lnet_nid_t **nids, __u32 *nnids)
838 int i = 0, count = 0, rc;
841 struct lnet_dlc_intf_descr *intf;
843 if (nw == NULL || nids == NULL)
844 return LUSTRE_CFG_RC_BAD_PARAM;
846 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network)
849 *nids = calloc(count, sizeof(lnet_nid_t));
851 return LUSTRE_CFG_RC_OUT_OF_MEM;
853 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network) {
854 memset(&ifr, 0, sizeof(ifr));
855 rc = socket_intf_query(SIOCGIFFLAGS, intf->intf_name, &ifr);
859 if ((ifr.ifr_flags & IFF_UP) == 0) {
860 rc = LUSTRE_CFG_RC_BAD_PARAM;
864 memset(&ifr, 0, sizeof(ifr));
865 rc = socket_intf_query(SIOCGIFADDR, intf->intf_name, &ifr);
869 ip = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
871 (*nids)[i] = LNET_MKNID(nw->nw_id, ip);
886 * called repeatedly until a match or no more ip range
888 * ip_range expression
889 * interface list with all the interface names.
890 * all the interfaces in the system.
892 * try to match the ip_range expr to one of the interfaces' IPs in
893 * the system. If we hit a patch for an interface. Check if that
894 * interface name is in the list.
896 * If there are more than one interface in the list, then make sure
897 * that the IPs for all of these interfaces match the ip ranges
900 * for each interface in intf_list
901 * look up the intf name in ifa
902 * if not there then no match
903 * check ip obtained from ifa against a match to any of the
905 * If no match, then fail
907 * The result is that all the interfaces have to match.
909 int lustre_lnet_match_ip_to_intf(struct ifaddrs *ifa,
910 struct list_head *intf_list,
911 struct list_head *ip_ranges)
915 struct lnet_dlc_intf_descr *intf_descr;
916 struct ifaddrs *ifaddr = ifa;
917 struct lustre_lnet_ip_range_descr *ip_range;
921 * if there are no explicit interfaces, and no ip ranges, then
922 * configure the first tcp interface we encounter.
924 if (list_empty(intf_list) && list_empty(ip_ranges)) {
925 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
926 if (ifaddr->ifa_addr == NULL)
929 if ((ifaddr->ifa_flags & IFF_UP) == 0)
932 family = ifaddr->ifa_addr->sa_family;
933 if (family == AF_INET) {
934 rc = lustre_lnet_add_intf_descr
935 (intf_list, ifaddr->ifa_name,
936 strlen(ifaddr->ifa_name));
938 if (rc != LUSTRE_CFG_RC_NO_ERR)
941 return LUSTRE_CFG_RC_MATCH;
944 return LUSTRE_CFG_RC_NO_MATCH;
948 * First interface which matches an IP pattern will be used
950 if (list_empty(intf_list)) {
952 * no interfaces provided in the rule, but an ip range is
953 * provided, so try and match an interface to the ip
956 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
957 if (ifaddr->ifa_addr == NULL)
960 if ((ifaddr->ifa_flags & IFF_UP) == 0)
963 family = ifaddr->ifa_addr->sa_family;
964 if (family == AF_INET) {
965 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->
968 list_for_each_entry(ip_range, ip_ranges,
970 rc = cfs_ip_addr_match(bswap_32(ip),
971 &ip_range->ipr_expr);
975 rc = lustre_lnet_add_intf_descr
976 (intf_list, ifaddr->ifa_name,
977 strlen(ifaddr->ifa_name));
979 if (rc != LUSTRE_CFG_RC_NO_ERR)
982 return LUSTRE_CFG_RC_MATCH;
989 * If an interface is explicitly specified the ip-range might or
990 * might not be specified. if specified the interface needs to match the
991 * ip-range. If no ip-range then the interfaces are
992 * automatically matched if they are all up.
993 * If > 1 interfaces all the interfaces must match for the NI to
996 list_for_each_entry(intf_descr, intf_list, intf_on_network) {
997 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
998 if (ifaddr->ifa_addr == NULL)
1001 family = ifaddr->ifa_addr->sa_family;
1002 if (family == AF_INET &&
1003 strcmp(intf_descr->intf_name,
1004 ifaddr->ifa_name) == 0)
1009 return LUSTRE_CFG_RC_NO_MATCH;
1011 if ((ifaddr->ifa_flags & IFF_UP) == 0)
1012 return LUSTRE_CFG_RC_NO_MATCH;
1014 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr.s_addr;
1016 list_for_each_entry(ip_range, ip_ranges, ipr_entry) {
1017 rc = cfs_ip_addr_match(bswap_32(ip), &ip_range->ipr_expr);
1021 return LUSTRE_CFG_RC_NO_MATCH;
1025 return LUSTRE_CFG_RC_MATCH;
1028 int lustre_lnet_resolve_ip2nets_rule(struct lustre_lnet_ip2nets *ip2nets,
1029 lnet_nid_t **nids, __u32 *nnids)
1031 struct ifaddrs *ifa;
1032 int rc = LUSTRE_CFG_RC_NO_ERR;
1034 rc = getifaddrs(&ifa);
1038 rc = lustre_lnet_match_ip_to_intf(ifa,
1039 &ip2nets->ip2nets_net.nw_intflist,
1040 &ip2nets->ip2nets_ip_ranges);
1041 if (rc != LUSTRE_CFG_RC_MATCH) {
1046 rc = lustre_lnet_intf2nids(&ip2nets->ip2nets_net, nids, nnids);
1047 if (rc != LUSTRE_CFG_RC_NO_ERR) {
1058 lustre_lnet_ioctl_config_ni(struct list_head *intf_list,
1059 struct lnet_ioctl_config_lnd_tunables *tunables,
1060 struct cfs_expr_list *global_cpts,
1061 lnet_nid_t *nids, char *err_str)
1064 struct lnet_ioctl_config_ni *conf;
1065 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1066 int rc = LUSTRE_CFG_RC_NO_ERR, i = 0;
1069 struct lnet_dlc_intf_descr *intf_descr;
1071 struct cfs_expr_list *cpt_expr;
1073 list_for_each_entry(intf_descr, intf_list,
1075 if (i == 0 && tunables != NULL)
1076 len = sizeof(struct lnet_ioctl_config_ni) +
1077 sizeof(struct lnet_ioctl_config_lnd_tunables);
1079 len = sizeof(struct lnet_ioctl_config_ni);
1081 data = calloc(1, len);
1082 conf = (struct lnet_ioctl_config_ni*) data;
1083 if (i == 0 && tunables != NULL)
1084 tun = (struct lnet_ioctl_config_lnd_tunables*)
1085 (data + sizeof(*conf));
1087 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1088 conf->lic_cfg_hdr.ioc_len = len;
1089 conf->lic_nid = nids[i];
1090 strncpy(conf->lic_ni_intf[0], intf_descr->intf_name,
1093 if (intf_descr->cpt_expr != NULL)
1094 cpt_expr = intf_descr->cpt_expr;
1095 else if (global_cpts != NULL)
1096 cpt_expr = global_cpts;
1100 if (cpt_expr != NULL) {
1101 count = cfs_expr_list_values(cpt_expr,
1102 LNET_MAX_SHOW_NUM_CPT,
1105 memcpy(conf->lic_cpts, cpt_array,
1106 sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1115 conf->lic_ncpts = count;
1117 if (i == 0 && tunables != NULL)
1118 /* TODO put in the LND tunables */
1119 memcpy(tun, tunables, sizeof(*tunables));
1121 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1126 "\"cannot add network: %s\"", strerror(errno));
1132 return LUSTRE_CFG_RC_NO_ERR;
1136 lustre_lnet_config_ip2nets(struct lustre_lnet_ip2nets *ip2nets,
1137 struct lnet_ioctl_config_lnd_tunables *tunables,
1138 struct cfs_expr_list *global_cpts,
1139 int seq_no, struct cYAML **err_rc)
1141 lnet_nid_t *nids = NULL;
1144 char err_str[LNET_MAX_STR_LEN];
1146 snprintf(err_str, sizeof(err_str), "\"success\"");
1148 if (ip2nets == NULL ||
1149 list_empty(&ip2nets->ip2nets_net.nw_intflist)) {
1152 "\"incomplete ip2nets information\"");
1153 rc = LUSTRE_CFG_RC_BAD_PARAM;
1157 rc = lustre_lnet_resolve_ip2nets_rule(ip2nets, &nids, &nnids);
1158 if (rc != LUSTRE_CFG_RC_NO_ERR && rc != LUSTRE_CFG_RC_MATCH) {
1161 "\"cannot resolve ip2nets rule\"");
1165 rc = lustre_lnet_ioctl_config_ni(&ip2nets->ip2nets_net.nw_intflist,
1166 tunables, global_cpts, nids,
1168 if (rc != LUSTRE_CFG_RC_NO_ERR)
1172 cYAML_build_error(rc, seq_no, ADD_CMD, "ip2nets", err_str, err_rc);
1176 int lustre_lnet_config_ni(struct lnet_dlc_network_descr *nw_descr,
1177 struct cfs_expr_list *global_cpts,
1179 struct lnet_ioctl_config_lnd_tunables *tunables,
1180 int seq_no, struct cYAML **err_rc)
1183 struct lnet_ioctl_config_ni *conf;
1184 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1185 char buf[LNET_MAX_STR_LEN];
1186 int rc = LUSTRE_CFG_RC_NO_ERR;
1187 char err_str[LNET_MAX_STR_LEN];
1188 lnet_nid_t *nids = NULL;
1192 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1195 snprintf(err_str, sizeof(err_str), "\"success\"");
1197 if (ip2net == NULL && nw_descr == NULL) {
1200 "\"mandatory parameters not specified.\"");
1201 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1205 if (ip2net != NULL && strlen(ip2net) >= sizeof(buf)) {
1208 "\"ip2net string too long %d\"",
1209 (int)strlen(ip2net));
1210 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1214 if (ip2net != NULL) {
1215 if (tunables != NULL)
1216 len = sizeof(struct lnet_ioctl_config_ni) +
1217 sizeof(struct lnet_ioctl_config_lnd_tunables);
1219 len = sizeof(struct lnet_ioctl_config_ni);
1220 data = calloc(1, len);
1221 conf = (struct lnet_ioctl_config_ni*) data;
1222 if (tunables != NULL)
1223 tun = (struct lnet_ioctl_config_lnd_tunables*)
1224 (data + sizeof(*conf));
1226 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1227 conf->lic_cfg_hdr.ioc_len = len;
1228 strncpy(conf->lic_legacy_ip2nets, ip2net,
1231 if (global_cpts != NULL) {
1232 count = cfs_expr_list_values(global_cpts,
1233 LNET_MAX_SHOW_NUM_CPT,
1236 memcpy(conf->lic_cpts, cpt_array,
1237 sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1246 conf->lic_ncpts = count;
1248 if (tunables != NULL)
1249 memcpy(tun, tunables, sizeof(*tunables));
1251 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1256 "\"cannot add network: %s\"", strerror(errno));
1263 if (LNET_NETTYP(nw_descr->nw_id) == LOLND)
1264 return LUSTRE_CFG_RC_NO_ERR;
1266 if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1269 "\"cannot parse net '%s'\"",
1270 libcfs_net2str(nw_descr->nw_id));
1271 rc = LUSTRE_CFG_RC_BAD_PARAM;
1275 if (list_empty(&nw_descr->nw_intflist)) {
1278 "\"no interface name provided\"");
1279 rc = LUSTRE_CFG_RC_BAD_PARAM;
1283 rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids);
1285 snprintf(err_str, sizeof(err_str),
1286 "\"bad parameter\"");
1287 rc = LUSTRE_CFG_RC_BAD_PARAM;
1291 rc = lustre_lnet_ioctl_config_ni(&nw_descr->nw_intflist,
1292 tunables, global_cpts, nids,
1296 if (nw_descr != NULL) {
1297 list_for_each_entry_safe(intf_descr, tmp,
1298 &nw_descr->nw_intflist,
1300 if (intf_descr->cpt_expr != NULL)
1301 cfs_expr_list_free(intf_descr->cpt_expr);
1306 cYAML_build_error(rc, seq_no, ADD_CMD, "net", err_str, err_rc);
1317 int lustre_lnet_del_ni(struct lnet_dlc_network_descr *nw_descr,
1318 int seq_no, struct cYAML **err_rc)
1320 struct lnet_ioctl_config_ni data;
1321 int rc = LUSTRE_CFG_RC_NO_ERR, i;
1322 char err_str[LNET_MAX_STR_LEN];
1323 lnet_nid_t *nids = NULL;
1325 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1327 if (LNET_NETTYP(nw_descr->nw_id) == LOLND)
1328 return LUSTRE_CFG_RC_NO_ERR;
1330 snprintf(err_str, sizeof(err_str), "\"success\"");
1332 if (nw_descr == NULL) {
1335 "\"missing mandatory parameter\"");
1336 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1340 if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1343 "\"cannot parse net '%s'\"",
1344 libcfs_net2str(nw_descr->nw_id));
1345 rc = LUSTRE_CFG_RC_BAD_PARAM;
1349 rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids);
1351 snprintf(err_str, sizeof(err_str),
1352 "\"bad parameter\"");
1353 rc = LUSTRE_CFG_RC_BAD_PARAM;
1358 * no interfaces just the nw_id is specified
1361 nids = calloc(1, sizeof(*nids));
1363 snprintf(err_str, sizeof(err_str),
1364 "\"out of memory\"");
1365 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1368 nids[0] = LNET_MKNID(nw_descr->nw_id, 0);
1372 for (i = 0; i < nnids; i++) {
1373 LIBCFS_IOC_INIT_V2(data, lic_cfg_hdr);
1374 data.lic_nid = nids[i];
1376 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_LOCAL_NI, &data);
1381 "\"cannot del network: %s\"", strerror(errno));
1385 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
1387 if (intf_descr->cpt_expr != NULL)
1388 cfs_expr_list_free(intf_descr->cpt_expr);
1393 cYAML_build_error(rc, seq_no, DEL_CMD, "net", err_str, err_rc);
1401 int lustre_lnet_show_net(char *nw, int detail, int seq_no,
1402 struct cYAML **show_rc, struct cYAML **err_rc)
1405 struct lnet_ioctl_config_ni *ni_data;
1406 struct lnet_ioctl_config_lnd_tunables *lnd;
1407 __u32 net = LNET_NIDNET(LNET_NID_ANY);
1408 __u32 prev_net = LNET_NIDNET(LNET_NID_ANY);
1409 int rc = LUSTRE_CFG_RC_OUT_OF_MEM, i, j;
1411 struct cYAML *root = NULL, *tunables = NULL,
1412 *net_node = NULL, *interfaces = NULL,
1413 *item = NULL, *first_seq = NULL,
1415 int str_buf_len = LNET_MAX_SHOW_NUM_CPT * 2;
1416 char str_buf[str_buf_len];
1418 char err_str[LNET_MAX_STR_LEN];
1419 bool exist = false, new_net = true;
1421 size_t buf_size = sizeof(*ni_data) + sizeof(*lnd);
1423 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1425 buf = calloc(1, buf_size);
1429 ni_data = (struct lnet_ioctl_config_ni *)buf;
1432 net = libcfs_str2net(nw);
1433 if (net == LNET_NIDNET(LNET_NID_ANY)) {
1436 "\"cannot parse net '%s'\"", nw);
1437 rc = LUSTRE_CFG_RC_BAD_PARAM;
1442 root = cYAML_create_object(NULL, NULL);
1446 net_node = cYAML_create_seq(root, "net");
1447 if (net_node == NULL)
1454 memset(buf, 0, buf_size);
1456 LIBCFS_IOC_INIT_V2(*ni_data, lic_cfg_hdr);
1458 * set the ioc_len to the proper value since INIT assumes
1461 ni_data->lic_cfg_hdr.ioc_len = buf_size;
1462 ni_data->lic_idx = i;
1464 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LOCAL_NI, ni_data);
1470 rc_net = LNET_NIDNET(ni_data->lic_nid);
1472 /* filter on provided data */
1473 if (net != LNET_NIDNET(LNET_NID_ANY) &&
1477 /* default rc to -1 in case we hit the goto */
1481 lnd = (struct lnet_ioctl_config_lnd_tunables *)ni_data->lic_bulk;
1483 if (rc_net != prev_net) {
1490 if (!cYAML_create_string(net_node, "net type",
1491 libcfs_net2str(rc_net)))
1494 tmp = cYAML_create_seq(net_node, "local NI(s)");
1500 /* create the tree to be printed. */
1501 item = cYAML_create_seq_item(tmp);
1505 if (first_seq == NULL)
1508 if (cYAML_create_string(item, "nid",
1509 libcfs_nid2str(ni_data->lic_nid)) == NULL)
1512 if (cYAML_create_string(item,
1514 (ni_data->lic_status ==
1515 LNET_NI_STATUS_UP) ?
1516 "up" : "down") == NULL)
1519 /* don't add interfaces unless there is at least one
1521 if (strlen(ni_data->lic_ni_intf[0]) > 0) {
1522 interfaces = cYAML_create_object(item, "interfaces");
1523 if (interfaces == NULL)
1526 for (j = 0; j < LNET_MAX_INTERFACES; j++) {
1527 if (strlen(ni_data->lic_ni_intf[j]) > 0) {
1529 sizeof(str_buf), "%d", j);
1530 if (cYAML_create_string(interfaces,
1532 ni_data->lic_ni_intf[j]) ==
1542 tunables = cYAML_create_object(item, "tunables");
1546 rc = lustre_net_show_tunables(tunables, &lnd->lt_cmn);
1547 if (rc != LUSTRE_CFG_RC_NO_ERR)
1550 tunables = cYAML_create_object(item, "lnd tunables");
1551 if (tunables == NULL)
1554 rc = lustre_ni_show_tunables(tunables, LNET_NETTYP(rc_net),
1556 if (rc != LUSTRE_CFG_RC_NO_ERR)
1559 if (cYAML_create_number(item, "tcp bonding",
1560 ni_data->lic_tcp_bonding)
1564 if (cYAML_create_number(item, "dev cpt",
1565 ni_data->lic_dev_cpt) == NULL)
1568 /* out put the CPTs in the format: "[x,x,x,...]" */
1569 limit = str_buf + str_buf_len - 3;
1570 pos += snprintf(pos, limit - pos, "\"[");
1571 for (j = 0 ; ni_data->lic_ncpts >= 1 &&
1572 j < ni_data->lic_ncpts &&
1574 pos += snprintf(pos, limit - pos,
1575 "%d", ni_data->lic_cpts[j]);
1576 if ((j + 1) < ni_data->lic_ncpts)
1577 pos += snprintf(pos, limit - pos, ",");
1579 pos += snprintf(pos, 3, "]\"");
1581 if (ni_data->lic_ncpts >= 1 &&
1582 cYAML_create_string(item, "CPT",
1588 /* Print out the net information only if show_rc is not provided */
1589 if (show_rc == NULL)
1590 cYAML_print_tree(root);
1592 if (l_errno != ENOENT) {
1595 "\"cannot get networks: %s\"",
1600 rc = LUSTRE_CFG_RC_NO_ERR;
1602 snprintf(err_str, sizeof(err_str), "\"success\"");
1604 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
1605 cYAML_free_tree(root);
1606 } else if (show_rc != NULL && *show_rc != NULL) {
1607 struct cYAML *show_node;
1608 /* find the net node, if one doesn't exist
1609 * then insert one. Otherwise add to the one there
1611 show_node = cYAML_get_object_item(*show_rc, "net");
1612 if (show_node != NULL && cYAML_is_sequence(show_node)) {
1613 cYAML_insert_child(show_node, first_seq);
1616 } else if (show_node == NULL) {
1617 cYAML_insert_sibling((*show_rc)->cy_child,
1621 cYAML_free_tree(root);
1627 cYAML_build_error(rc, seq_no, SHOW_CMD, "net", err_str, err_rc);
1632 int lustre_lnet_enable_routing(int enable, int seq_no, struct cYAML **err_rc)
1634 struct lnet_ioctl_config_data data;
1635 int rc = LUSTRE_CFG_RC_NO_ERR;
1636 char err_str[LNET_MAX_STR_LEN];
1638 snprintf(err_str, sizeof(err_str), "\"success\"");
1640 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1641 data.cfg_config_u.cfg_buffers.buf_enable = (enable) ? 1 : 0;
1643 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIG_RTR, &data);
1648 "\"cannot %s routing %s\"",
1649 (enable) ? "enable" : "disable", strerror(errno));
1654 cYAML_build_error(rc, seq_no,
1655 (enable) ? ADD_CMD : DEL_CMD,
1656 "routing", err_str, err_rc);
1661 int lustre_lnet_config_numa_range(int range, int seq_no, struct cYAML **err_rc)
1663 struct lnet_ioctl_numa_range data;
1664 int rc = LUSTRE_CFG_RC_NO_ERR;
1665 char err_str[LNET_MAX_STR_LEN];
1667 snprintf(err_str, sizeof(err_str), "\"success\"");
1672 "\"range must be >= 0\"");
1673 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1677 LIBCFS_IOC_INIT_V2(data, nr_hdr);
1678 data.nr_range = range;
1680 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_SET_NUMA_RANGE, &data);
1685 "\"cannot configure buffers: %s\"", strerror(errno));
1690 cYAML_build_error(rc, seq_no, ADD_CMD, "numa_range", err_str, err_rc);
1695 int lustre_lnet_config_buffers(int tiny, int small, int large, int seq_no,
1696 struct cYAML **err_rc)
1698 struct lnet_ioctl_config_data data;
1699 int rc = LUSTRE_CFG_RC_NO_ERR;
1700 char err_str[LNET_MAX_STR_LEN];
1702 snprintf(err_str, sizeof(err_str), "\"success\"");
1704 /* -1 indicates to ignore changes to this field */
1705 if (tiny < -1 || small < -1 || large < -1) {
1708 "\"tiny, small and large must be >= 0\"");
1709 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1713 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1714 data.cfg_config_u.cfg_buffers.buf_tiny = tiny;
1715 data.cfg_config_u.cfg_buffers.buf_small = small;
1716 data.cfg_config_u.cfg_buffers.buf_large = large;
1718 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_BUF, &data);
1723 "\"cannot configure buffers: %s\"", strerror(errno));
1728 cYAML_build_error(rc, seq_no, ADD_CMD, "buf", err_str, err_rc);
1733 int lustre_lnet_show_routing(int seq_no, struct cYAML **show_rc,
1734 struct cYAML **err_rc)
1736 struct lnet_ioctl_config_data *data;
1737 struct lnet_ioctl_pool_cfg *pool_cfg = NULL;
1738 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1741 char *pools[LNET_NRBPOOLS] = {"tiny", "small", "large"};
1742 int buf_count[LNET_NRBPOOLS] = {0};
1743 struct cYAML *root = NULL, *pools_node = NULL,
1744 *type_node = NULL, *item = NULL, *cpt = NULL,
1745 *first_seq = NULL, *buffers = NULL;
1747 char err_str[LNET_MAX_STR_LEN];
1748 char node_name[LNET_MAX_STR_LEN];
1751 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1753 buf = calloc(1, sizeof(*data) + sizeof(*pool_cfg));
1757 data = (struct lnet_ioctl_config_data *)buf;
1759 root = cYAML_create_object(NULL, NULL);
1763 pools_node = cYAML_create_seq(root, "routing");
1764 if (pools_node == NULL)
1768 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
1769 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
1770 sizeof(struct lnet_ioctl_pool_cfg);
1771 data->cfg_count = i;
1773 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_BUF, data);
1781 pool_cfg = (struct lnet_ioctl_pool_cfg *)data->cfg_bulk;
1783 snprintf(node_name, sizeof(node_name), "cpt[%d]", i);
1784 item = cYAML_create_seq_item(pools_node);
1788 if (first_seq == NULL)
1791 cpt = cYAML_create_object(item, node_name);
1795 /* create the tree and print */
1796 for (j = 0; j < LNET_NRBPOOLS; j++) {
1797 type_node = cYAML_create_object(cpt, pools[j]);
1798 if (type_node == NULL)
1800 if (cYAML_create_number(type_node, "npages",
1801 pool_cfg->pl_pools[j].pl_npages)
1804 if (cYAML_create_number(type_node, "nbuffers",
1805 pool_cfg->pl_pools[j].
1806 pl_nbuffers) == NULL)
1808 if (cYAML_create_number(type_node, "credits",
1809 pool_cfg->pl_pools[j].
1810 pl_credits) == NULL)
1812 if (cYAML_create_number(type_node, "mincredits",
1813 pool_cfg->pl_pools[j].
1814 pl_mincredits) == NULL)
1816 /* keep track of the total count for each of the
1817 * tiny, small and large buffers */
1818 buf_count[j] += pool_cfg->pl_pools[j].pl_nbuffers;
1822 if (pool_cfg != NULL) {
1823 item = cYAML_create_seq_item(pools_node);
1827 if (cYAML_create_number(item, "enable", pool_cfg->pl_routing) ==
1832 /* create a buffers entry in the show. This is necessary so that
1833 * if the YAML output is used to configure a node, the buffer
1834 * configuration takes hold */
1835 buffers = cYAML_create_object(root, "buffers");
1836 if (buffers == NULL)
1839 for (i = 0; i < LNET_NRBPOOLS; i++) {
1840 if (cYAML_create_number(buffers, pools[i], buf_count[i]) == NULL)
1844 if (show_rc == NULL)
1845 cYAML_print_tree(root);
1847 if (l_errno != ENOENT) {
1850 "\"cannot get routing information: %s\"",
1855 rc = LUSTRE_CFG_RC_NO_ERR;
1857 snprintf(err_str, sizeof(err_str), "\"success\"");
1858 rc = LUSTRE_CFG_RC_NO_ERR;
1862 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
1863 cYAML_free_tree(root);
1864 } else if (show_rc != NULL && *show_rc != NULL) {
1865 struct cYAML *routing_node;
1866 /* there should exist only one routing block and one
1867 * buffers block. If there already exists a previous one
1868 * then don't add another */
1869 routing_node = cYAML_get_object_item(*show_rc, "routing");
1870 if (routing_node == NULL) {
1871 cYAML_insert_sibling((*show_rc)->cy_child,
1875 cYAML_free_tree(root);
1881 cYAML_build_error(rc, seq_no, SHOW_CMD, "routing", err_str, err_rc);
1886 int lustre_lnet_show_peer(char *knid, int seq_no, struct cYAML **show_rc,
1887 struct cYAML **err_rc)
1889 struct lnet_ioctl_peer_cfg *peer_info;
1890 struct lnet_peer_ni_credit_info *lpni_cri;
1891 int rc = LUSTRE_CFG_RC_OUT_OF_MEM, ncpt = 0, i = 0, j = 0;
1893 struct cYAML *root = NULL, *peer = NULL, *peer_ni = NULL,
1894 *first_seq = NULL, *peer_root = NULL, *tmp = NULL;
1895 char err_str[LNET_MAX_STR_LEN];
1896 lnet_nid_t prev_primary_nid = LNET_NID_ANY, primary_nid = LNET_NID_ANY;
1897 char *data = calloc(sizeof(*peer_info) + sizeof(*lpni_cri), 1);
1898 bool new_peer = true;
1900 snprintf(err_str, sizeof(err_str),
1901 "\"out of memory\"");
1906 peer_info = (struct lnet_ioctl_peer_cfg *)data;
1908 /* create struct cYAML root object */
1909 root = cYAML_create_object(NULL, NULL);
1913 peer_root = cYAML_create_seq(root, "peer");
1914 if (peer_root == NULL)
1918 primary_nid = libcfs_str2nid(knid);
1922 memset(data, 0, sizeof(*peer_info) + sizeof(*lpni_cri));
1923 LIBCFS_IOC_INIT_V2(*peer_info, prcfg_hdr);
1924 peer_info->prcfg_hdr.ioc_len = sizeof(*peer_info) +
1926 peer_info->prcfg_idx = i;
1928 rc = l_ioctl(LNET_DEV_ID,
1929 IOC_LIBCFS_GET_PEER_NI, peer_info);
1935 if (primary_nid != LNET_NID_ANY &&
1936 primary_nid != peer_info->prcfg_key_nid)
1939 lpni_cri = (struct lnet_peer_ni_credit_info*)peer_info->prcfg_bulk;
1941 peer = cYAML_create_seq_item(peer_root);
1945 if (peer_info->prcfg_key_nid != prev_primary_nid) {
1946 prev_primary_nid = peer_info->prcfg_key_nid;
1951 lnet_nid_t pnid = peer_info->prcfg_key_nid;
1952 if (cYAML_create_string(peer, "primary nid",
1953 libcfs_nid2str(pnid))
1956 tmp = cYAML_create_seq(peer, "peer ni");
1962 if (first_seq == NULL)
1965 peer_ni = cYAML_create_seq_item(tmp);
1966 if (peer_ni == NULL)
1969 if (cYAML_create_string(peer_ni, "nid",
1971 (peer_info->prcfg_cfg_nid))
1975 if (cYAML_create_string(peer_ni, "state",
1976 lpni_cri->cr_aliveness)
1980 if (cYAML_create_number(peer_ni, "refcount",
1981 lpni_cri->cr_refcount) == NULL)
1984 if (cYAML_create_number(peer_ni, "max_ni_tx_credits",
1985 lpni_cri->cr_ni_peer_tx_credits)
1989 if (cYAML_create_number(peer_ni, "available_tx_credits",
1990 lpni_cri->cr_peer_tx_credits)
1994 if (cYAML_create_number(peer_ni, "available_rtr_credits",
1995 lpni_cri->cr_peer_rtr_credits)
1999 if (cYAML_create_number(peer_ni, "min_rtr_credits",
2000 lpni_cri->cr_peer_min_rtr_credits)
2004 if (cYAML_create_number(peer_ni, "tx_q_num_of_buf",
2005 lpni_cri->cr_peer_tx_qnob)
2010 if (l_errno != ENOENT) {
2013 "\"cannot get peer information: %s\"",
2022 /* print output iff show_rc is not provided */
2023 if (show_rc == NULL)
2024 cYAML_print_tree(root);
2026 snprintf(err_str, sizeof(err_str), "\"success\"");
2027 rc = LUSTRE_CFG_RC_NO_ERR;
2030 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2031 cYAML_free_tree(root);
2032 } else if (show_rc != NULL && *show_rc != NULL) {
2033 struct cYAML *show_node;
2034 /* find the peer node, if one doesn't exist then
2035 * insert one. Otherwise add to the one there
2037 show_node = cYAML_get_object_item(*show_rc,
2039 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2040 cYAML_insert_child(show_node, first_seq);
2043 } else if (show_node == NULL) {
2044 cYAML_insert_sibling((*show_rc)->cy_child,
2048 cYAML_free_tree(root);
2054 cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
2060 int lustre_lnet_show_numa_range(int seq_no, struct cYAML **show_rc,
2061 struct cYAML **err_rc)
2063 struct lnet_ioctl_numa_range data;
2064 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2066 char err_str[LNET_MAX_STR_LEN];
2067 struct cYAML *root = NULL, *range = NULL;
2069 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2071 LIBCFS_IOC_INIT_V2(data, nr_hdr);
2073 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NUMA_RANGE, &data);
2078 "\"cannot get numa range: %s\"",
2084 root = cYAML_create_object(NULL, NULL);
2088 range = cYAML_create_object(root, "numa");
2092 if (cYAML_create_number(range, "range",
2093 data.nr_range) == NULL)
2096 if (show_rc == NULL)
2097 cYAML_print_tree(root);
2099 snprintf(err_str, sizeof(err_str), "\"success\"");
2101 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2102 cYAML_free_tree(root);
2103 } else if (show_rc != NULL && *show_rc != NULL) {
2104 cYAML_insert_sibling((*show_rc)->cy_child,
2111 cYAML_build_error(rc, seq_no, SHOW_CMD, "numa", err_str, err_rc);
2116 int lustre_lnet_show_stats(int seq_no, struct cYAML **show_rc,
2117 struct cYAML **err_rc)
2119 struct lnet_ioctl_lnet_stats data;
2120 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2122 char err_str[LNET_MAX_STR_LEN];
2123 struct cYAML *root = NULL, *stats = NULL;
2125 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2127 LIBCFS_IOC_INIT_V2(data, st_hdr);
2129 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LNET_STATS, &data);
2134 "\"cannot get lnet statistics: %s\"",
2140 root = cYAML_create_object(NULL, NULL);
2144 stats = cYAML_create_object(root, "statistics");
2148 if (cYAML_create_number(stats, "msgs_alloc",
2149 data.st_cntrs.msgs_alloc) == NULL)
2152 if (cYAML_create_number(stats, "msgs_max",
2153 data.st_cntrs.msgs_max) == NULL)
2156 if (cYAML_create_number(stats, "errors",
2157 data.st_cntrs.errors) == NULL)
2160 if (cYAML_create_number(stats, "send_count",
2161 data.st_cntrs.send_count) == NULL)
2164 if (cYAML_create_number(stats, "recv_count",
2165 data.st_cntrs.recv_count) == NULL)
2168 if (cYAML_create_number(stats, "route_count",
2169 data.st_cntrs.route_count) == NULL)
2172 if (cYAML_create_number(stats, "drop_count",
2173 data.st_cntrs.drop_count) == NULL)
2176 if (cYAML_create_number(stats, "send_length",
2177 data.st_cntrs.send_length) == NULL)
2180 if (cYAML_create_number(stats, "recv_length",
2181 data.st_cntrs.recv_length) == NULL)
2184 if (cYAML_create_number(stats, "route_length",
2185 data.st_cntrs.route_length) == NULL)
2188 if (cYAML_create_number(stats, "drop_length",
2189 data.st_cntrs.drop_length) == NULL)
2192 if (show_rc == NULL)
2193 cYAML_print_tree(root);
2195 snprintf(err_str, sizeof(err_str), "\"success\"");
2197 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2198 cYAML_free_tree(root);
2199 } else if (show_rc != NULL && *show_rc != NULL) {
2200 cYAML_insert_sibling((*show_rc)->cy_child,
2207 cYAML_build_error(rc, seq_no, SHOW_CMD, "statistics", err_str, err_rc);
2212 typedef int (*cmd_handler_t)(struct cYAML *tree,
2213 struct cYAML **show_rc,
2214 struct cYAML **err_rc);
2216 static int handle_yaml_config_route(struct cYAML *tree, struct cYAML **show_rc,
2217 struct cYAML **err_rc)
2219 struct cYAML *net, *gw, *hop, *prio, *seq_no;
2221 net = cYAML_get_object_item(tree, "net");
2222 gw = cYAML_get_object_item(tree, "gateway");
2223 hop = cYAML_get_object_item(tree, "hop");
2224 prio = cYAML_get_object_item(tree, "priority");
2225 seq_no = cYAML_get_object_item(tree, "seq_no");
2227 return lustre_lnet_config_route((net) ? net->cy_valuestring : NULL,
2228 (gw) ? gw->cy_valuestring : NULL,
2229 (hop) ? hop->cy_valueint : -1,
2230 (prio) ? prio->cy_valueint : -1,
2231 (seq_no) ? seq_no->cy_valueint : -1,
2235 static void yaml_free_string_array(char **array, int num)
2238 char **sub_array = array;
2240 for (i = 0; i < num; i++) {
2241 if (*sub_array != NULL)
2250 * 0: <intf_name>['['<expr>']']
2251 * 1: <intf_name>['['<expr>']']
2253 static int yaml_copy_intf_info(struct cYAML *intf_tree,
2254 struct lnet_dlc_network_descr *nw_descr)
2256 struct cYAML *child = NULL;
2257 int intf_num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
2258 struct lnet_dlc_intf_descr *intf_descr, *tmp;
2260 if (intf_tree == NULL || nw_descr == NULL)
2261 return LUSTRE_CFG_RC_BAD_PARAM;
2263 /* now grab all the interfaces and their cpts */
2264 child = intf_tree->cy_child;
2265 while (child != NULL) {
2266 if (child->cy_valuestring == NULL) {
2267 child = child->cy_next;
2271 if (strlen(child->cy_valuestring) >= LNET_MAX_STR_LEN)
2274 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist,
2275 child->cy_valuestring,
2276 strlen(child->cy_valuestring));
2277 if (rc != LUSTRE_CFG_RC_NO_ERR)
2281 child = child->cy_next;
2285 return LUSTRE_CFG_RC_MISSING_PARAM;
2290 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
2292 if (intf_descr->cpt_expr != NULL)
2293 cfs_expr_list_free(intf_descr->cpt_expr);
2301 yaml_extract_cmn_tunables(struct cYAML *tree,
2302 struct lnet_ioctl_config_lnd_cmn_tunables *tunables,
2303 int *num_global_cpts,
2304 struct cfs_expr_list **global_cpts)
2306 struct cYAML *tun, *item, *smp;
2308 tun = cYAML_get_object_item(tree, "tunables");
2310 item = cYAML_get_object_item(tun, "peer_timeout");
2312 tunables->lct_peer_timeout = item->cy_valueint;
2313 item = cYAML_get_object_item(tun, "peer_credits");
2315 tunables->lct_peer_tx_credits = item->cy_valueint;
2316 item = cYAML_get_object_item(tun, "peer_buffer_credits");
2318 tunables->lct_peer_rtr_credits = item->cy_valueint;
2319 item = cYAML_get_object_item(tun, "credits");
2321 tunables->lct_max_tx_credits = item->cy_valueint;
2322 smp = cYAML_get_object_item(tun, "CPT");
2325 cfs_expr_list_parse(smp->cy_valuestring,
2326 strlen(smp->cy_valuestring),
2327 0, UINT_MAX, global_cpts);
2337 yaml_extract_tunables(struct cYAML *tree,
2338 struct lnet_ioctl_config_lnd_tunables *tunables,
2339 int *num_global_cpts,
2340 struct cfs_expr_list **global_cpts,
2345 rc = yaml_extract_cmn_tunables(tree, &tunables->lt_cmn,
2346 num_global_cpts, global_cpts);
2351 lustre_yaml_extract_lnd_tunables(tree, net_type,
2359 * - net type: <net>[<NUM>]
2361 * - nid: <ip>@<net>[<NUM>]
2364 * 0: <intf_name>['['<expr>']']
2365 * 1: <intf_name>['['<expr>']']
2367 * peer_timeout: <NUM>
2368 * peer_credits: <NUM>
2369 * peer_buffer_credits: <NUM>
2372 * peercredits_hiw: <NUM>
2373 * map_on_demand: <NUM>
2374 * concurrent_sends: <NUM>
2375 * fmr_pool_size: <NUM>
2376 * fmr_flush_trigger: <NUM>
2379 * At least one interface is required. If no interfaces are provided the
2380 * network interface can not be configured.
2382 static int handle_yaml_config_ni(struct cYAML *tree, struct cYAML **show_rc,
2383 struct cYAML **err_rc)
2385 struct cYAML *net, *intf, *seq_no, *ip2net = NULL, *local_nis = NULL,
2387 int num_entries = 0, num_global_cpts = 0, rc;
2388 struct lnet_dlc_network_descr nw_descr;
2389 struct cfs_expr_list *global_cpts = NULL;
2390 struct lnet_ioctl_config_lnd_tunables tunables;
2393 memset(&tunables, 0, sizeof(tunables));
2395 INIT_LIST_HEAD(&nw_descr.network_on_rule);
2396 INIT_LIST_HEAD(&nw_descr.nw_intflist);
2398 ip2net = cYAML_get_object_item(tree, "ip2net");
2399 net = cYAML_get_object_item(tree, "net type");
2401 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
2404 * if neither net nor ip2nets are present, then we can not
2405 * configure the network.
2407 if (!net && !ip2net)
2408 return LUSTRE_CFG_RC_MISSING_PARAM;
2410 local_nis = cYAML_get_object_item(tree, "local NI(s)");
2411 if (local_nis == NULL)
2412 return LUSTRE_CFG_RC_MISSING_PARAM;
2414 if (!cYAML_is_sequence(local_nis))
2415 return LUSTRE_CFG_RC_BAD_PARAM;
2417 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
2418 intf = cYAML_get_object_item(item, "interfaces");
2421 num_entries = yaml_copy_intf_info(intf, &nw_descr);
2422 if (num_entries <= 0) {
2423 cYAML_build_error(num_entries, -1, "ni", "add",
2424 "bad interface list",
2426 return LUSTRE_CFG_RC_BAD_PARAM;
2430 found = yaml_extract_tunables(tree, &tunables, &num_global_cpts,
2432 LNET_NETTYP(nw_descr.nw_id));
2433 seq_no = cYAML_get_object_item(tree, "seq_no");
2435 rc = lustre_lnet_config_ni(&nw_descr,
2436 (num_global_cpts > 0) ? global_cpts: NULL,
2437 (ip2net) ? ip2net->cy_valuestring : NULL,
2438 (found) ? &tunables: NULL,
2439 (seq_no) ? seq_no->cy_valueint : -1,
2442 if (global_cpts != NULL)
2443 cfs_expr_list_free(global_cpts);
2450 * - net-spec: <tcp|o2ib|gni>[NUM]
2452 * 0: <intf name>['['<expr>']']
2453 * 1: <intf name>['['<expr>']']
2455 * 0: <expr.expr.expr.expr>
2456 * 1: <expr.expr.expr.expr>
2458 static int handle_yaml_config_ip2nets(struct cYAML *tree,
2459 struct cYAML **show_rc,
2460 struct cYAML **err_rc)
2462 struct cYAML *net, *ip_range, *item = NULL, *intf = NULL,
2464 struct lustre_lnet_ip2nets ip2nets;
2465 struct lustre_lnet_ip_range_descr *ip_range_descr = NULL,
2467 int rc = LUSTRE_CFG_RC_NO_ERR, num_global_cpts = 0;
2468 struct cfs_expr_list *global_cpts = NULL;
2469 struct cfs_expr_list *el, *el_tmp;
2470 struct lnet_ioctl_config_lnd_tunables tunables;
2471 struct lnet_dlc_intf_descr *intf_descr, *intf_tmp;
2474 memset(&tunables, 0, sizeof(tunables));
2476 /* initialize all lists */
2477 INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
2478 INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
2479 INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
2481 net = cYAML_get_object_item(tree, "net-spec");
2483 return LUSTRE_CFG_RC_BAD_PARAM;
2485 if (net != NULL && net->cy_valuestring == NULL)
2486 return LUSTRE_CFG_RC_BAD_PARAM;
2488 /* assign the network id */
2489 ip2nets.ip2nets_net.nw_id = libcfs_str2net(net->cy_valuestring);
2490 if (ip2nets.ip2nets_net.nw_id == LNET_NID_ANY)
2491 return LUSTRE_CFG_RC_BAD_PARAM;
2493 seq_no = cYAML_get_object_item(tree, "seq_no");
2495 intf = cYAML_get_object_item(tree, "interfaces");
2497 rc = yaml_copy_intf_info(intf, &ip2nets.ip2nets_net);
2499 return LUSTRE_CFG_RC_BAD_PARAM;
2502 ip_range = cYAML_get_object_item(tree, "ip-range");
2503 if (ip_range != NULL) {
2504 item = ip_range->cy_child;
2505 while (item != NULL) {
2506 if (item->cy_valuestring == NULL) {
2507 item = item->cy_next;
2511 rc = lustre_lnet_add_ip_range(&ip2nets.ip2nets_ip_ranges,
2512 item->cy_valuestring);
2514 if (rc != LUSTRE_CFG_RC_NO_ERR)
2517 item = item->cy_next;
2521 found = yaml_extract_tunables(tree, &tunables, &num_global_cpts,
2523 LNET_NETTYP(ip2nets.ip2nets_net.nw_id));
2525 rc = lustre_lnet_config_ip2nets(&ip2nets,
2526 (found) ? &tunables : NULL,
2527 (num_global_cpts > 0) ? global_cpts : NULL,
2528 (seq_no) ? seq_no->cy_valueint : -1,
2532 * don't stop because there was no match. Continue processing the
2533 * rest of the rules. If non-match then nothing is configured
2535 if (rc == LUSTRE_CFG_RC_NO_MATCH)
2536 rc = LUSTRE_CFG_RC_NO_ERR;
2538 list_for_each_entry_safe(intf_descr, intf_tmp,
2539 &ip2nets.ip2nets_net.nw_intflist,
2541 if (intf_descr->cpt_expr != NULL)
2542 cfs_expr_list_free(intf_descr->cpt_expr);
2546 list_for_each_entry_safe(ip_range_descr, tmp,
2547 &ip2nets.ip2nets_ip_ranges,
2549 list_for_each_entry_safe(el, el_tmp, &ip_range_descr->ipr_expr,
2551 cfs_expr_list_free(el);
2552 free(ip_range_descr);
2558 static int handle_yaml_del_ni(struct cYAML *tree, struct cYAML **show_rc,
2559 struct cYAML **err_rc)
2561 struct cYAML *net = NULL, *intf = NULL, *seq_no = NULL, *item = NULL,
2563 int num_entries, rc;
2564 struct lnet_dlc_network_descr nw_descr;
2566 INIT_LIST_HEAD(&nw_descr.network_on_rule);
2567 INIT_LIST_HEAD(&nw_descr.nw_intflist);
2569 net = cYAML_get_object_item(tree, "net type");
2571 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
2573 local_nis = cYAML_get_object_item(tree, "local NI(s)");
2574 if (local_nis == NULL)
2575 return LUSTRE_CFG_RC_MISSING_PARAM;
2577 if (!cYAML_is_sequence(local_nis))
2578 return LUSTRE_CFG_RC_BAD_PARAM;
2580 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
2581 intf = cYAML_get_object_item(item, "interfaces");
2584 num_entries = yaml_copy_intf_info(intf, &nw_descr);
2585 if (num_entries <= 0) {
2586 cYAML_build_error(num_entries, -1, "ni", "add",
2587 "bad interface list",
2589 return LUSTRE_CFG_RC_BAD_PARAM;
2593 seq_no = cYAML_get_object_item(tree, "seq_no");
2595 rc = lustre_lnet_del_ni((net) ? &nw_descr : NULL,
2596 (seq_no) ? seq_no->cy_valueint : -1,
2602 static int yaml_copy_peer_nids(struct cYAML *tree, char ***nidsppp)
2604 struct cYAML *nids_entry = NULL, *child;
2606 int num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
2608 nids_entry = cYAML_get_object_item(tree, "nids");
2609 if (nids_entry != NULL) {
2611 child = nids_entry->cy_child;
2612 while (child != NULL) {
2614 child = child->cy_next;
2618 return LUSTRE_CFG_RC_MISSING_PARAM;
2620 nids = calloc(sizeof(*nids) * num, 1);
2622 return LUSTRE_CFG_RC_OUT_OF_MEM;
2624 /* now grab all the nids */
2625 child = nids_entry->cy_child;
2627 while (child != NULL) {
2628 nids[num] = calloc(strlen(child->cy_valuestring) + 1,
2630 if (nids[num] == NULL) {
2631 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2634 strncpy(nids[num], child->cy_valuestring,
2635 strlen(child->cy_valuestring));
2636 child = child->cy_next;
2641 rc = LUSTRE_CFG_RC_MISSING_PARAM;
2650 yaml_free_string_array(nids, num);
2655 static int handle_yaml_config_peer(struct cYAML *tree, struct cYAML **show_rc,
2656 struct cYAML **err_rc)
2660 struct cYAML *seq_no, *key_nid;
2662 num = yaml_copy_peer_nids(tree, &nids);
2666 seq_no = cYAML_get_object_item(tree, "seq_no");
2667 key_nid = cYAML_get_object_item(tree, "key_nid");
2669 rc = lustre_lnet_config_peer_nid((key_nid) ? key_nid->cy_valuestring : NULL,
2671 (seq_no) ? seq_no->cy_valueint : -1,
2674 yaml_free_string_array(nids, num);
2678 static int handle_yaml_del_peer(struct cYAML *tree, struct cYAML **show_rc,
2679 struct cYAML **err_rc)
2683 struct cYAML *seq_no, *key_nid;
2685 num = yaml_copy_peer_nids(tree, &nids);
2689 seq_no = cYAML_get_object_item(tree, "seq_no");
2690 key_nid = cYAML_get_object_item(tree, "key_nid");
2692 rc = lustre_lnet_del_peer_nid((key_nid) ? key_nid->cy_valuestring : NULL,
2694 (seq_no) ? seq_no->cy_valueint : -1,
2697 yaml_free_string_array(nids, num);
2701 static int handle_yaml_config_buffers(struct cYAML *tree,
2702 struct cYAML **show_rc,
2703 struct cYAML **err_rc)
2706 struct cYAML *tiny, *small, *large, *seq_no;
2708 tiny = cYAML_get_object_item(tree, "tiny");
2709 small = cYAML_get_object_item(tree, "small");
2710 large = cYAML_get_object_item(tree, "large");
2711 seq_no = cYAML_get_object_item(tree, "seq_no");
2713 rc = lustre_lnet_config_buffers((tiny) ? tiny->cy_valueint : -1,
2714 (small) ? small->cy_valueint : -1,
2715 (large) ? large->cy_valueint : -1,
2716 (seq_no) ? seq_no->cy_valueint : -1,
2722 static int handle_yaml_config_routing(struct cYAML *tree,
2723 struct cYAML **show_rc,
2724 struct cYAML **err_rc)
2726 int rc = LUSTRE_CFG_RC_NO_ERR;
2727 struct cYAML *seq_no, *enable;
2729 seq_no = cYAML_get_object_item(tree, "seq_no");
2730 enable = cYAML_get_object_item(tree, "enable");
2733 rc = lustre_lnet_enable_routing(enable->cy_valueint,
2735 seq_no->cy_valueint : -1,
2742 static int handle_yaml_del_route(struct cYAML *tree, struct cYAML **show_rc,
2743 struct cYAML **err_rc)
2747 struct cYAML *seq_no;
2749 net = cYAML_get_object_item(tree, "net");
2750 gw = cYAML_get_object_item(tree, "gateway");
2751 seq_no = cYAML_get_object_item(tree, "seq_no");
2753 return lustre_lnet_del_route((net) ? net->cy_valuestring : NULL,
2754 (gw) ? gw->cy_valuestring : NULL,
2755 (seq_no) ? seq_no->cy_valueint : -1,
2759 static int handle_yaml_del_routing(struct cYAML *tree, struct cYAML **show_rc,
2760 struct cYAML **err_rc)
2762 struct cYAML *seq_no;
2764 seq_no = cYAML_get_object_item(tree, "seq_no");
2766 return lustre_lnet_enable_routing(0, (seq_no) ?
2767 seq_no->cy_valueint : -1,
2771 static int handle_yaml_show_route(struct cYAML *tree, struct cYAML **show_rc,
2772 struct cYAML **err_rc)
2778 struct cYAML *detail;
2779 struct cYAML *seq_no;
2781 net = cYAML_get_object_item(tree, "net");
2782 gw = cYAML_get_object_item(tree, "gateway");
2783 hop = cYAML_get_object_item(tree, "hop");
2784 prio = cYAML_get_object_item(tree, "priority");
2785 detail = cYAML_get_object_item(tree, "detail");
2786 seq_no = cYAML_get_object_item(tree, "seq_no");
2788 return lustre_lnet_show_route((net) ? net->cy_valuestring : NULL,
2789 (gw) ? gw->cy_valuestring : NULL,
2790 (hop) ? hop->cy_valueint : -1,
2791 (prio) ? prio->cy_valueint : -1,
2792 (detail) ? detail->cy_valueint : 0,
2793 (seq_no) ? seq_no->cy_valueint : -1,
2798 static int handle_yaml_show_net(struct cYAML *tree, struct cYAML **show_rc,
2799 struct cYAML **err_rc)
2801 struct cYAML *net, *detail, *seq_no;
2803 net = cYAML_get_object_item(tree, "net");
2804 detail = cYAML_get_object_item(tree, "detail");
2805 seq_no = cYAML_get_object_item(tree, "seq_no");
2807 return lustre_lnet_show_net((net) ? net->cy_valuestring : NULL,
2808 (detail) ? detail->cy_valueint : 0,
2809 (seq_no) ? seq_no->cy_valueint : -1,
2814 static int handle_yaml_show_routing(struct cYAML *tree, struct cYAML **show_rc,
2815 struct cYAML **err_rc)
2817 struct cYAML *seq_no;
2819 seq_no = cYAML_get_object_item(tree, "seq_no");
2821 return lustre_lnet_show_routing((seq_no) ? seq_no->cy_valueint : -1,
2825 static int handle_yaml_show_credits(struct cYAML *tree, struct cYAML **show_rc,
2826 struct cYAML **err_rc)
2828 struct cYAML *seq_no, *key_nid;
2830 seq_no = cYAML_get_object_item(tree, "seq_no");
2831 key_nid = cYAML_get_object_item(tree, "key_nid");
2833 return lustre_lnet_show_peer((key_nid) ? key_nid->cy_valuestring : NULL,
2834 (seq_no) ? seq_no->cy_valueint : -1,
2838 static int handle_yaml_show_stats(struct cYAML *tree, struct cYAML **show_rc,
2839 struct cYAML **err_rc)
2841 struct cYAML *seq_no;
2843 seq_no = cYAML_get_object_item(tree, "seq_no");
2845 return lustre_lnet_show_stats((seq_no) ? seq_no->cy_valueint : -1,
2849 struct lookup_cmd_hdlr_tbl {
2854 static struct lookup_cmd_hdlr_tbl lookup_config_tbl[] = {
2855 {"route", handle_yaml_config_route},
2856 {"net", handle_yaml_config_ni},
2857 {"ip2nets", handle_yaml_config_ip2nets},
2858 {"peer", handle_yaml_config_peer},
2859 {"routing", handle_yaml_config_routing},
2860 {"buffers", handle_yaml_config_buffers},
2864 static struct lookup_cmd_hdlr_tbl lookup_del_tbl[] = {
2865 {"route", handle_yaml_del_route},
2866 {"net", handle_yaml_del_ni},
2867 {"peer", handle_yaml_del_peer},
2868 {"routing", handle_yaml_del_routing},
2872 static struct lookup_cmd_hdlr_tbl lookup_show_tbl[] = {
2873 {"route", handle_yaml_show_route},
2874 {"net", handle_yaml_show_net},
2875 {"buffers", handle_yaml_show_routing},
2876 {"routing", handle_yaml_show_routing},
2877 {"credits", handle_yaml_show_credits},
2878 {"statistics", handle_yaml_show_stats},
2882 static cmd_handler_t lookup_fn(char *key,
2883 struct lookup_cmd_hdlr_tbl *tbl)
2889 for (i = 0; tbl[i].name != NULL; i++) {
2890 if (strncmp(key, tbl[i].name, strlen(tbl[i].name)) == 0)
2897 static int lustre_yaml_cb_helper(char *f, struct lookup_cmd_hdlr_tbl *table,
2898 struct cYAML **show_rc, struct cYAML **err_rc)
2900 struct cYAML *tree, *item = NULL, *head, *child;
2902 char err_str[LNET_MAX_STR_LEN];
2903 int rc = LUSTRE_CFG_RC_NO_ERR, return_rc = LUSTRE_CFG_RC_NO_ERR;
2905 tree = cYAML_build_tree(f, NULL, 0, err_rc, false);
2907 return LUSTRE_CFG_RC_BAD_PARAM;
2909 child = tree->cy_child;
2910 while (child != NULL) {
2911 cb = lookup_fn(child->cy_string, table);
2913 snprintf(err_str, sizeof(err_str),
2914 "\"call back for '%s' not found\"",
2916 cYAML_build_error(LUSTRE_CFG_RC_BAD_PARAM, -1,
2917 "yaml", "helper", err_str, err_rc);
2921 if (cYAML_is_sequence(child)) {
2922 while ((head = cYAML_get_next_seq_item(child, &item))
2924 rc = cb(head, show_rc, err_rc);
2925 if (rc != LUSTRE_CFG_RC_NO_ERR)
2929 rc = cb(child, show_rc, err_rc);
2930 if (rc != LUSTRE_CFG_RC_NO_ERR)
2934 child = child->cy_next;
2938 cYAML_free_tree(tree);
2943 int lustre_yaml_config(char *f, struct cYAML **err_rc)
2945 return lustre_yaml_cb_helper(f, lookup_config_tbl,
2949 int lustre_yaml_del(char *f, struct cYAML **err_rc)
2951 return lustre_yaml_cb_helper(f, lookup_del_tbl,
2955 int lustre_yaml_show(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
2957 return lustre_yaml_cb_helper(f, lookup_show_tbl,
2961 int lustre_lnet_send_dbg_task(enum lnet_dbg_task dbg_task,
2962 struct lnet_dbg_task_info *dbg_info,
2963 struct cYAML **show_rc,
2964 struct cYAML **err_rc)
2966 struct lnet_ioctl_dbg *dbg;
2967 struct lnet_dbg_task_info *info;
2968 int rc = LUSTRE_CFG_RC_NO_ERR;
2969 char err_str[LNET_MAX_STR_LEN];
2971 snprintf(err_str, sizeof(err_str), "\"success\"");
2973 dbg = calloc(1, sizeof(*dbg) + sizeof(*info));
2975 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2976 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2980 info = (struct lnet_dbg_task_info *)dbg->dbg_bulk;
2982 LIBCFS_IOC_INIT_V2(*dbg, dbg_hdr);
2984 dbg->dbg_task = dbg_task;
2986 memcpy(info, dbg_info, sizeof(*info));
2988 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DBG, dbg);
2993 "\"debug task failed %s\"", strerror(errno));
2998 cYAML_build_error(rc, -1, DBG_CMD,
2999 "debug", err_str, err_rc);