4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of the
9 * License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 * Copyright (c) 2014, 2016, Intel Corporation.
24 * Amir Shehata <amir.shehata@intel.com>
29 * 1. APIs that take the actual parameters expanded. This is for other
30 * entities that would like to link against the library and call the APIs
31 * directly without having to form an intermediate representation.
32 * 2. APIs that take a YAML file and parses out the information there and
33 * calls the APIs mentioned in 1
44 #include <sys/ioctl.h>
46 #include <libcfs/util/ioctl.h>
47 #include <lnet/lnetctl.h>
48 #include <lnet/socklnd.h>
50 #include <lnet/lnet.h>
51 #include <sys/types.h>
53 #include "liblnetconfig.h"
56 #define CONFIG_CMD "configure"
57 #define UNCONFIG_CMD "unconfigure"
60 #define SHOW_CMD "show"
62 #define MAX_NUM_IPS 128
65 * lustre_lnet_ip_range_descr
66 * Describes an IP range.
67 * Each octect is an expression
69 struct lustre_lnet_ip_range_descr {
70 struct list_head ipr_entry;
71 struct list_head ipr_expr;
76 * Describes an ip2nets rule. This can be on a list of rules.
78 struct lustre_lnet_ip2nets {
79 struct lnet_dlc_network_descr ip2nets_net;
80 struct list_head ip2nets_ip_ranges;
85 * frees the memory allocated for an intf descriptor.
87 void free_intf_descr(struct lnet_dlc_intf_descr *intf_descr)
92 if (intf_descr->cpt_expr != NULL)
93 cfs_expr_list_free(intf_descr->cpt_expr);
98 * lustre_lnet_add_ip_range
100 * given a string of the format:
101 * <expr.expr.expr.expr> parse each expr into
102 * a lustre_lnet_ip_range_descr structure and insert on the list.
104 * This function is called from
105 * YAML on each ip-range.
106 * As a result of lnetctl command
107 * When building a NID or P2P selection rules
109 int lustre_lnet_add_ip_range(struct list_head *list, char *str_ip_range)
111 struct lustre_lnet_ip_range_descr *ip_range;
114 ip_range = calloc(1, sizeof(*ip_range));
115 if (ip_range == NULL)
116 return LUSTRE_CFG_RC_OUT_OF_MEM;
118 INIT_LIST_HEAD(&ip_range->ipr_entry);
119 INIT_LIST_HEAD(&ip_range->ipr_expr);
121 rc = cfs_ip_addr_parse(str_ip_range, strlen(str_ip_range),
122 &ip_range->ipr_expr);
124 return LUSTRE_CFG_RC_BAD_PARAM;
126 list_add_tail(&ip_range->ipr_entry, list);
128 return LUSTRE_CFG_RC_NO_ERR;
131 int lustre_lnet_add_intf_descr(struct list_head *list, char *intf, int len)
133 char *open_sq_bracket = NULL, *close_sq_bracket = NULL,
135 struct lnet_dlc_intf_descr *intf_descr = NULL;
137 char intf_string[LNET_MAX_STR_LEN];
139 if (len >= LNET_MAX_STR_LEN)
140 return LUSTRE_CFG_RC_BAD_PARAM;
142 strncpy(intf_string, intf, len);
143 intf_string[len] = '\0';
145 intf_descr = calloc(1, sizeof(*intf_descr));
146 if (intf_descr == NULL)
147 return LUSTRE_CFG_RC_OUT_OF_MEM;
149 INIT_LIST_HEAD(&intf_descr->intf_on_network);
151 intf_name = intf_string;
152 open_sq_bracket = strchr(intf_string, '[');
153 if (open_sq_bracket != NULL) {
154 close_sq_bracket = strchr(intf_string, ']');
155 if (close_sq_bracket == NULL) {
157 return LUSTRE_CFG_RC_BAD_PARAM;
159 rc = cfs_expr_list_parse(open_sq_bracket,
160 strlen(open_sq_bracket), 0, UINT_MAX,
161 &intf_descr->cpt_expr);
164 return LUSTRE_CFG_RC_BAD_PARAM;
166 strncpy(intf_descr->intf_name, intf_name,
167 open_sq_bracket - intf_name);
168 intf_descr->intf_name[open_sq_bracket - intf_name] = '\0';
170 strcpy(intf_descr->intf_name, intf_name);
171 intf_descr->cpt_expr = NULL;
174 list_add_tail(&intf_descr->intf_on_network, list);
176 return LUSTRE_CFG_RC_NO_ERR;
179 void lustre_lnet_init_nw_descr(struct lnet_dlc_network_descr *nw_descr)
181 if (nw_descr != NULL) {
182 INIT_LIST_HEAD(&nw_descr->network_on_rule);
183 INIT_LIST_HEAD(&nw_descr->nw_intflist);
187 static char *get_next_delimiter_in_nid(char *str, char sep)
191 /* first find the '@' */
192 at = strchr(str, '@');
196 /* now that you found the at find the sep after */
197 comma = strchr(at, sep);
201 int lustre_lnet_parse_nids(char *nids, char **array, int size,
205 char *comma = nids, *cur, *entry;
207 int i, len, start = 0, finish = 0;
209 if (nids == NULL || strlen(nids) == 0)
212 /* count the number or new nids, by counting the number of comma*/
214 comma = get_next_delimiter_in_nid(comma, ',');
224 * if the array is not NULL allocate a large enough array to house
225 * the old and new entries
227 new_array = calloc(sizeof(char*),
228 (size > 0) ? size + num_nids : num_nids);
233 /* parse our the new nids and add them to the tail of the array */
236 start = (size > 0) ? size: 0;
237 finish = (size > 0) ? size + num_nids : num_nids;
238 for (i = start; i < finish; i++) {
239 comma = get_next_delimiter_in_nid(comma, ',');
242 * the length of the string to be parsed out is
243 * from cur to end of string. So it's good enough
246 len = strlen(cur) + 1;
248 /* length of the string is comma - cur */
249 len = (comma - cur) + 1;
251 entry = calloc(1, len);
253 finish = i > 0 ? i - 1: 0;
256 strncpy(entry, cur, len - 1);
258 new_array[i] = entry;
265 /* add the old entries in the array and delete the old array*/
266 for (i = 0; i < size; i++)
267 new_array[i] = array[i];
272 *out_array = new_array;
277 for (i = start; i < finish; i++)
287 * <intf>[<expr>], <intf>[<expr>],..
289 int lustre_lnet_parse_interfaces(char *intf_str,
290 struct lnet_dlc_network_descr *nw_descr)
295 char *cur = intf_str, *next = NULL;
296 char *end = intf_str + strlen(intf_str);
298 struct lnet_dlc_intf_descr *intf_descr, *tmp;
300 if (nw_descr == NULL)
301 return LUSTRE_CFG_RC_BAD_PARAM;
304 open_square = strchr(cur, '[');
305 if (open_square != NULL) {
306 close_square = strchr(cur, ']');
307 if (close_square == NULL) {
308 rc = LUSTRE_CFG_RC_BAD_PARAM;
312 comma = strchr(cur, ',');
313 if (comma != NULL && comma > close_square) {
315 len = next - close_square;
321 comma = strchr(cur, ',');
331 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist, cur, len);
332 if (rc != LUSTRE_CFG_RC_NO_ERR)
338 return LUSTRE_CFG_RC_NO_ERR;
341 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
343 list_del(&intf_descr->intf_on_network);
344 free_intf_descr(intf_descr);
350 int lustre_lnet_config_lib_init(void)
352 return register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH);
355 void lustre_lnet_config_lib_uninit(void)
357 unregister_ioc_dev(LNET_DEV_ID);
360 int lustre_lnet_config_ni_system(bool up, bool load_ni_from_mod,
361 int seq_no, struct cYAML **err_rc)
363 struct libcfs_ioctl_data data;
366 char err_str[LNET_MAX_STR_LEN];
368 snprintf(err_str, sizeof(err_str), "\"Success\"");
370 LIBCFS_IOC_INIT(data);
372 /* Reverse logic is used here in order not to change
373 * the lctl utility */
374 data.ioc_flags = load_ni_from_mod ? 0 : 1;
376 opc = up ? IOC_LIBCFS_CONFIGURE : IOC_LIBCFS_UNCONFIGURE;
378 rc = l_ioctl(LNET_DEV_ID, opc, &data);
382 "\"LNet %s error: %s\"", (up) ? "configure" :
383 "unconfigure", strerror(errno));
387 cYAML_build_error(rc, seq_no, (up) ? CONFIG_CMD : UNCONFIG_CMD,
388 "lnet", err_str, err_rc);
393 static int dispatch_peer_ni_cmd(lnet_nid_t pnid, lnet_nid_t nid, __u32 cmd,
394 struct lnet_ioctl_peer_cfg *data,
395 char *err_str, char *cmd_str)
399 data->prcfg_prim_nid = pnid;
400 data->prcfg_cfg_nid = nid;
402 rc = l_ioctl(LNET_DEV_ID, cmd, data);
407 "\"cannot %s peer ni: %s\"",
408 (cmd_str) ? cmd_str : "add", strerror(errno));
409 err_str[LNET_MAX_STR_LEN - 1] = '\0';
415 static void lustre_lnet_clean_ip2nets(struct lustre_lnet_ip2nets *ip2nets)
417 struct lustre_lnet_ip_range_descr *ipr, *tmp;
418 struct cfs_expr_list *el, *el_tmp;
420 list_for_each_entry_safe(ipr, tmp,
421 &ip2nets->ip2nets_ip_ranges,
423 list_del(&ipr->ipr_entry);
424 list_for_each_entry_safe(el, el_tmp, &ipr->ipr_expr,
426 list_del(&el->el_link);
427 cfs_expr_list_free(el);
434 * returns an rc < 0 if there is an error
435 * otherwise it returns the number IPs generated
436 * it also has out params: net - network name
438 static int lnet_expr2ips(char *nidstr, __u32 *ip_list,
439 struct lustre_lnet_ip2nets *ip2nets,
440 __u32 *net, char *err_str)
442 struct lustre_lnet_ip_range_descr *ipr;
444 int ip_idx = MAX_NUM_IPS - 1;
445 int ip_range_len, rc = LUSTRE_CFG_RC_NO_ERR;
447 char ip_range[LNET_MAX_STR_LEN];
449 /* separate the two components of the NID */
451 comp2 = strchr(nidstr, '@');
455 "\"cannot parse NID %s\"", nidstr);
456 err_str[LNET_MAX_STR_LEN - 1] = '\0';
457 rc = LUSTRE_CFG_RC_BAD_PARAM;
461 /* length of the expected ip-range */
462 ip_range_len = comp2 - comp1;
463 if (ip_range_len >= LNET_MAX_STR_LEN) {
466 "\"cannot parse ip_range '%s'\"", ip_range);
467 err_str[LNET_MAX_STR_LEN - 1] = '\0';
468 rc = LUSTRE_CFG_RC_BAD_PARAM;
472 /* move beyond '@' */
476 * if the net component is either o2ib or tcp then we expect
477 * an IP range which could only be a single IP address.
480 *net = libcfs_str2net(comp2);
481 net_type = LNET_NETTYP(*net);
482 /* expression support is for o2iblnd and socklnd only */
483 if (net_type != O2IBLND && net_type != SOCKLND)
484 return LUSTRE_CFG_RC_SKIP;
486 strncpy(ip_range, comp1, ip_range_len);
487 ip_range[ip_range_len] = '\0';
488 ip2nets->ip2nets_net.nw_id = *net;
490 rc = lustre_lnet_add_ip_range(&ip2nets->ip2nets_ip_ranges, ip_range);
491 if (rc != LUSTRE_CFG_RC_NO_ERR) {
494 "\"cannot parse ip_range '%s'\"", ip_range);
495 err_str[LNET_MAX_STR_LEN - 1] = '\0';
496 rc = LUSTRE_CFG_RC_BAD_PARAM;
501 * Generate all the IP Addresses from the parsed range. For sanity
502 * we allow only a max of MAX_NUM_IPS nids to be configured for
505 list_for_each_entry(ipr, &ip2nets->ip2nets_ip_ranges, ipr_entry)
506 ip_idx = cfs_ip_addr_range_gen(ip_list, MAX_NUM_IPS,
509 if (ip_idx == MAX_NUM_IPS - 1) {
510 snprintf(err_str, LNET_MAX_STR_LEN,
511 "no NIDs provided for configuration");
512 err_str[LNET_MAX_STR_LEN - 1] = '\0';
513 rc = LUSTRE_CFG_RC_NO_MATCH;
515 } else if (ip_idx == -1) {
516 rc = LUSTRE_CFG_RC_LAST_ELEM;
525 static int lustre_lnet_handle_peer_ip2nets(char **nid, int num_nids, bool mr,
526 bool range, __u32 cmd,
527 char *cmd_type, char *err_str)
529 __u32 net = LNET_NIDNET(LNET_NID_ANY);
531 int i, j, rc = LUSTRE_CFG_RC_NO_ERR;
532 __u32 ip_list[MAX_NUM_IPS];
533 struct lustre_lnet_ip2nets ip2nets;
534 struct lnet_ioctl_peer_cfg data;
536 lnet_nid_t prim_nid = LNET_NID_ANY;
538 /* initialize all lists */
539 INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
540 INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
541 INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
543 /* each nid entry is an expression */
544 for (i = 0; i < num_nids; i++) {
545 if (!range && i == 0)
546 prim_nid = libcfs_str2nid(nid[0]);
548 prim_nid = LNET_NID_ANY;
550 rc = lnet_expr2ips(nid[i], ip_list, &ip2nets, &net, err_str);
551 if (rc == LUSTRE_CFG_RC_SKIP)
553 else if (rc == LUSTRE_CFG_RC_LAST_ELEM)
555 else if (rc < LUSTRE_CFG_RC_NO_ERR)
560 for (j = MAX_NUM_IPS - 1; j > ip_idx; j--) {
561 peer_nid = LNET_MKNID(net, ip_list[j]);
562 if (peer_nid == LNET_NID_ANY) {
565 "\"cannot parse NID\"");
566 err_str[LNET_MAX_STR_LEN - 1] = '\0';
567 rc = LUSTRE_CFG_RC_BAD_PARAM;
571 LIBCFS_IOC_INIT_V2(data, prcfg_hdr);
574 if (prim_nid == LNET_NID_ANY && j == MAX_NUM_IPS - 1) {
576 peer_nid = LNET_NID_ANY;
579 if (!range && num_nids > 1 && i == 0 &&
580 cmd == IOC_LIBCFS_DEL_PEER_NI)
582 else if (!range && i == 0)
583 peer_nid = LNET_NID_ANY;
586 * If prim_nid is not provided then the first nid in the
587 * list becomes the prim_nid. First time round the loop
588 * use LNET_NID_ANY for the first parameter, then use
589 * nid[0] as the key nid after wards
591 rc = dispatch_peer_ni_cmd(prim_nid, peer_nid, cmd,
592 &data, err_str, cmd_type);
597 * we just deleted the entire peer using the
598 * primary_nid. So don't bother iterating through
599 * the rest of the nids
601 if (prim_nid != LNET_NID_ANY &&
602 peer_nid == LNET_NID_ANY &&
603 cmd == IOC_LIBCFS_DEL_PEER_NI)
607 lustre_lnet_clean_ip2nets(&ip2nets);
611 lustre_lnet_clean_ip2nets(&ip2nets);
615 int lustre_lnet_config_peer_nid(char *pnid, char **nid, int num_nids,
616 bool mr, bool ip2nets, int seq_no,
617 struct cYAML **err_rc)
619 int rc = LUSTRE_CFG_RC_NO_ERR;
620 char err_str[LNET_MAX_STR_LEN] = {0};
621 char **nid_array = NULL;
623 snprintf(err_str, sizeof(err_str), "\"Success\"");
626 rc = lustre_lnet_handle_peer_ip2nets(nid, num_nids, mr,
627 ip2nets, IOC_LIBCFS_ADD_PEER_NI,
633 if (libcfs_str2nid(pnid) == LNET_NID_ANY) {
634 snprintf(err_str, sizeof(err_str),
635 "bad primary NID: '%s'",
637 rc = LUSTRE_CFG_RC_MISSING_PARAM;
643 nid_array = calloc(sizeof(*nid_array), num_nids);
645 snprintf(err_str, sizeof(err_str),
647 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
651 memcpy(&nid_array[1], nid, sizeof(*nid) * (num_nids - 1));
654 rc = lustre_lnet_handle_peer_ip2nets((pnid) ? nid_array : nid,
655 num_nids, mr, ip2nets,
656 IOC_LIBCFS_ADD_PEER_NI, ADD_CMD,
665 cYAML_build_error(rc, seq_no, ADD_CMD, "peer_ni", err_str, err_rc);
669 int lustre_lnet_del_peer_nid(char *pnid, char **nid, int num_nids,
670 bool ip2nets, int seq_no, struct cYAML **err_rc)
672 int rc = LUSTRE_CFG_RC_NO_ERR;
673 char err_str[LNET_MAX_STR_LEN] = {0};
674 char **nid_array = NULL;
676 snprintf(err_str, sizeof(err_str), "\"Success\"");
679 rc = lustre_lnet_handle_peer_ip2nets(nid, num_nids, false,
680 ip2nets, IOC_LIBCFS_DEL_PEER_NI,
686 snprintf(err_str, sizeof(err_str),
687 "\"Primary nid is not provided\"");
688 rc = LUSTRE_CFG_RC_MISSING_PARAM;
690 } else if (!ip2nets) {
691 if (libcfs_str2nid(pnid) == LNET_NID_ANY) {
692 rc = LUSTRE_CFG_RC_BAD_PARAM;
693 snprintf(err_str, sizeof(err_str),
701 nid_array = calloc(sizeof(*nid_array), num_nids);
703 snprintf(err_str, sizeof(err_str),
705 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
709 memcpy(&nid_array[1], nid, sizeof(*nid) * (num_nids - 1));
711 rc = lustre_lnet_handle_peer_ip2nets(nid_array, num_nids, false,
712 ip2nets, IOC_LIBCFS_DEL_PEER_NI,
721 cYAML_build_error(rc, seq_no, DEL_CMD, "peer_ni", err_str, err_rc);
725 int lustre_lnet_config_route(char *nw, char *gw, int hops, int prio,
726 int seq_no, struct cYAML **err_rc)
728 struct lnet_ioctl_config_data data;
729 lnet_nid_t gateway_nid;
730 int rc = LUSTRE_CFG_RC_NO_ERR;
731 __u32 net = LNET_NIDNET(LNET_NID_ANY);
732 char err_str[LNET_MAX_STR_LEN];
734 snprintf(err_str, sizeof(err_str), "\"Success\"");
736 if (nw == NULL || gw == NULL) {
739 "\"missing mandatory parameter(s): '%s'\"",
740 (nw == NULL && gw == NULL) ? "network, gateway" :
741 (nw == NULL) ? "network" : "gateway");
742 rc = LUSTRE_CFG_RC_MISSING_PARAM;
746 net = libcfs_str2net(nw);
747 if (net == LNET_NIDNET(LNET_NID_ANY)) {
750 "\"cannot parse net %s\"", nw);
751 rc = LUSTRE_CFG_RC_BAD_PARAM;
755 gateway_nid = libcfs_str2nid(gw);
756 if (gateway_nid == LNET_NID_ANY) {
759 "\"cannot parse gateway NID '%s'\"", gw);
760 rc = LUSTRE_CFG_RC_BAD_PARAM;
765 /* hops is undefined */
766 hops = LNET_UNDEFINED_HOPS;
767 } else if (hops < 1 || hops > 255) {
770 "\"invalid hop count %d, must be between 1 and 255\"",
772 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
778 } else if (prio < 0) {
781 "\"invalid priority %d, must be greater than 0\"",
783 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
787 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
789 data.cfg_config_u.cfg_route.rtr_hop = hops;
790 data.cfg_config_u.cfg_route.rtr_priority = prio;
791 data.cfg_nid = gateway_nid;
793 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
798 "\"cannot add route: %s\"", strerror(errno));
803 cYAML_build_error(rc, seq_no, ADD_CMD, "route", err_str, err_rc);
808 int lustre_lnet_del_route(char *nw, char *gw,
809 int seq_no, struct cYAML **err_rc)
811 struct lnet_ioctl_config_data data;
812 lnet_nid_t gateway_nid;
813 int rc = LUSTRE_CFG_RC_NO_ERR;
814 __u32 net = LNET_NIDNET(LNET_NID_ANY);
815 char err_str[LNET_MAX_STR_LEN];
817 snprintf(err_str, sizeof(err_str), "\"Success\"");
819 if (nw == NULL || gw == NULL) {
822 "\"missing mandatory parameter(s): '%s'\"",
823 (nw == NULL && gw == NULL) ? "network, gateway" :
824 (nw == NULL) ? "network" : "gateway");
825 rc = LUSTRE_CFG_RC_MISSING_PARAM;
829 net = libcfs_str2net(nw);
830 if (net == LNET_NIDNET(LNET_NID_ANY)) {
833 "\"cannot parse net '%s'\"", nw);
834 rc = LUSTRE_CFG_RC_BAD_PARAM;
838 gateway_nid = libcfs_str2nid(gw);
839 if (gateway_nid == LNET_NID_ANY) {
842 "\"cannot parse gateway NID '%s'\"", gw);
843 rc = LUSTRE_CFG_RC_BAD_PARAM;
847 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
849 data.cfg_nid = gateway_nid;
851 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
856 "\"cannot delete route: %s\"", strerror(errno));
861 cYAML_build_error(rc, seq_no, DEL_CMD, "route", err_str, err_rc);
866 int lustre_lnet_show_route(char *nw, char *gw, int hops, int prio, int detail,
867 int seq_no, struct cYAML **show_rc,
868 struct cYAML **err_rc)
870 struct lnet_ioctl_config_data data;
871 lnet_nid_t gateway_nid;
872 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
874 __u32 net = LNET_NIDNET(LNET_NID_ANY);
876 struct cYAML *root = NULL, *route = NULL, *item = NULL;
877 struct cYAML *first_seq = NULL;
878 char err_str[LNET_MAX_STR_LEN];
881 snprintf(err_str, sizeof(err_str),
882 "\"out of memory\"");
885 net = libcfs_str2net(nw);
886 if (net == LNET_NIDNET(LNET_NID_ANY)) {
889 "\"cannot parse net '%s'\"", nw);
890 rc = LUSTRE_CFG_RC_BAD_PARAM;
895 /* show all routes without filtering on net */
896 net = LNET_NIDNET(LNET_NID_ANY);
900 gateway_nid = libcfs_str2nid(gw);
901 if (gateway_nid == LNET_NID_ANY) {
904 "\"cannot parse gateway NID '%s'\"", gw);
905 rc = LUSTRE_CFG_RC_BAD_PARAM;
909 /* show all routes with out filtering on gateway */
910 gateway_nid = LNET_NID_ANY;
912 if ((hops < 1 && hops != -1) || hops > 255) {
915 "\"invalid hop count %d, must be between 0 and 256\"",
917 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
921 /* create struct cYAML root object */
922 root = cYAML_create_object(NULL, NULL);
926 route = cYAML_create_seq(root, "route");
931 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
934 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
940 /* filter on provided data */
941 if (net != LNET_NIDNET(LNET_NID_ANY) &&
945 if (gateway_nid != LNET_NID_ANY &&
946 gateway_nid != data.cfg_nid)
950 hops != data.cfg_config_u.cfg_route.rtr_hop)
954 prio != data.cfg_config_u.cfg_route.rtr_priority)
957 /* default rc to -1 incase we hit the goto */
961 item = cYAML_create_seq_item(route);
965 if (first_seq == NULL)
968 if (cYAML_create_string(item, "net",
969 libcfs_net2str(data.cfg_net)) == NULL)
972 if (cYAML_create_string(item, "gateway",
973 libcfs_nid2str(data.cfg_nid)) == NULL)
977 if (cYAML_create_number(item, "hop",
978 (int) data.cfg_config_u.
979 cfg_route.rtr_hop) ==
983 if (cYAML_create_number(item, "priority",
985 cfg_route.rtr_priority) == NULL)
988 if (cYAML_create_string(item, "state",
989 data.cfg_config_u.cfg_route.
991 "up" : "down") == NULL)
996 /* print output iff show_rc is not provided */
998 cYAML_print_tree(root);
1000 if (l_errno != ENOENT) {
1003 "\"cannot get routes: %s\"",
1008 rc = LUSTRE_CFG_RC_NO_ERR;
1010 snprintf(err_str, sizeof(err_str), "\"success\"");
1012 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
1013 cYAML_free_tree(root);
1014 } else if (show_rc != NULL && *show_rc != NULL) {
1015 struct cYAML *show_node;
1016 /* find the route node, if one doesn't exist then
1017 * insert one. Otherwise add to the one there
1019 show_node = cYAML_get_object_item(*show_rc, "route");
1020 if (show_node != NULL && cYAML_is_sequence(show_node)) {
1021 cYAML_insert_child(show_node, first_seq);
1024 } else if (show_node == NULL) {
1025 cYAML_insert_sibling((*show_rc)->cy_child,
1029 cYAML_free_tree(root);
1035 cYAML_build_error(rc, seq_no, SHOW_CMD, "route", err_str, err_rc);
1040 static int socket_intf_query(int request, char *intf,
1046 if (strlen(intf) >= IFNAMSIZ || ifr == NULL)
1047 return LUSTRE_CFG_RC_BAD_PARAM;
1049 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
1051 return LUSTRE_CFG_RC_BAD_PARAM;
1053 strcpy(ifr->ifr_name, intf);
1054 rc = ioctl(sockfd, request, ifr);
1056 rc = LUSTRE_CFG_RC_BAD_PARAM;
1064 * for each interface in the array of interfaces find the IP address of
1065 * that interface, create its nid and add it to an array of NIDs.
1066 * Stop if any of the interfaces is down
1068 static int lustre_lnet_intf2nids(struct lnet_dlc_network_descr *nw,
1069 lnet_nid_t **nids, __u32 *nnids)
1071 int i = 0, count = 0, rc;
1074 struct lnet_dlc_intf_descr *intf;
1076 if (nw == NULL || nids == NULL)
1077 return LUSTRE_CFG_RC_BAD_PARAM;
1079 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network)
1082 *nids = calloc(count, sizeof(lnet_nid_t));
1084 return LUSTRE_CFG_RC_OUT_OF_MEM;
1086 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network) {
1087 memset(&ifr, 0, sizeof(ifr));
1088 rc = socket_intf_query(SIOCGIFFLAGS, intf->intf_name, &ifr);
1092 if ((ifr.ifr_flags & IFF_UP) == 0) {
1093 rc = LUSTRE_CFG_RC_BAD_PARAM;
1097 memset(&ifr, 0, sizeof(ifr));
1098 rc = socket_intf_query(SIOCGIFADDR, intf->intf_name, &ifr);
1102 ip = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
1104 (*nids)[i] = LNET_MKNID(nw->nw_id, ip);
1119 * called repeatedly until a match or no more ip range
1121 * ip_range expression
1122 * interface list with all the interface names.
1123 * all the interfaces in the system.
1125 * try to match the ip_range expr to one of the interfaces' IPs in
1126 * the system. If we hit a patch for an interface. Check if that
1127 * interface name is in the list.
1129 * If there are more than one interface in the list, then make sure
1130 * that the IPs for all of these interfaces match the ip ranges
1133 * for each interface in intf_list
1134 * look up the intf name in ifa
1135 * if not there then no match
1136 * check ip obtained from ifa against a match to any of the
1138 * If no match, then fail
1140 * The result is that all the interfaces have to match.
1142 int lustre_lnet_match_ip_to_intf(struct ifaddrs *ifa,
1143 struct list_head *intf_list,
1144 struct list_head *ip_ranges)
1148 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1149 struct ifaddrs *ifaddr = ifa;
1150 struct lustre_lnet_ip_range_descr *ip_range;
1154 * if there are no explicit interfaces, and no ip ranges, then
1155 * configure the first tcp interface we encounter.
1157 if (list_empty(intf_list) && list_empty(ip_ranges)) {
1158 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1159 if (ifaddr->ifa_addr == NULL)
1162 if ((ifaddr->ifa_flags & IFF_UP) == 0)
1165 family = ifaddr->ifa_addr->sa_family;
1166 if (family == AF_INET &&
1167 strcmp(ifaddr->ifa_name, "lo") != 0) {
1168 rc = lustre_lnet_add_intf_descr
1169 (intf_list, ifaddr->ifa_name,
1170 strlen(ifaddr->ifa_name));
1172 if (rc != LUSTRE_CFG_RC_NO_ERR)
1175 return LUSTRE_CFG_RC_MATCH;
1178 return LUSTRE_CFG_RC_NO_MATCH;
1182 * First interface which matches an IP pattern will be used
1184 if (list_empty(intf_list)) {
1186 * no interfaces provided in the rule, but an ip range is
1187 * provided, so try and match an interface to the ip
1190 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1191 if (ifaddr->ifa_addr == NULL)
1194 if ((ifaddr->ifa_flags & IFF_UP) == 0)
1197 family = ifaddr->ifa_addr->sa_family;
1198 if (family == AF_INET) {
1199 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->
1202 list_for_each_entry(ip_range, ip_ranges,
1204 rc = cfs_ip_addr_match(bswap_32(ip),
1205 &ip_range->ipr_expr);
1209 rc = lustre_lnet_add_intf_descr
1210 (intf_list, ifaddr->ifa_name,
1211 strlen(ifaddr->ifa_name));
1213 if (rc != LUSTRE_CFG_RC_NO_ERR)
1219 if (!list_empty(intf_list))
1220 return LUSTRE_CFG_RC_MATCH;
1222 return LUSTRE_CFG_RC_NO_MATCH;
1226 * If an interface is explicitly specified the ip-range might or
1227 * might not be specified. if specified the interface needs to match the
1228 * ip-range. If no ip-range then the interfaces are
1229 * automatically matched if they are all up.
1230 * If > 1 interfaces all the interfaces must match for the NI to
1233 list_for_each_entry_safe(intf_descr, tmp, intf_list, intf_on_network) {
1234 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1235 if (ifaddr->ifa_addr == NULL)
1238 family = ifaddr->ifa_addr->sa_family;
1239 if (family == AF_INET &&
1240 strcmp(intf_descr->intf_name,
1241 ifaddr->ifa_name) == 0)
1245 if (ifaddr == NULL) {
1246 list_del(&intf_descr->intf_on_network);
1247 free_intf_descr(intf_descr);
1251 if ((ifaddr->ifa_flags & IFF_UP) == 0) {
1252 list_del(&intf_descr->intf_on_network);
1253 free_intf_descr(intf_descr);
1257 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr.s_addr;
1260 list_for_each_entry(ip_range, ip_ranges, ipr_entry) {
1261 rc = cfs_ip_addr_match(bswap_32(ip), &ip_range->ipr_expr);
1267 /* no match for this interface */
1268 list_del(&intf_descr->intf_on_network);
1269 free_intf_descr(intf_descr);
1273 return LUSTRE_CFG_RC_MATCH;
1276 int lustre_lnet_resolve_ip2nets_rule(struct lustre_lnet_ip2nets *ip2nets,
1277 lnet_nid_t **nids, __u32 *nnids)
1279 struct ifaddrs *ifa;
1280 int rc = LUSTRE_CFG_RC_NO_ERR;
1282 rc = getifaddrs(&ifa);
1286 rc = lustre_lnet_match_ip_to_intf(ifa,
1287 &ip2nets->ip2nets_net.nw_intflist,
1288 &ip2nets->ip2nets_ip_ranges);
1289 if (rc != LUSTRE_CFG_RC_MATCH) {
1294 rc = lustre_lnet_intf2nids(&ip2nets->ip2nets_net, nids, nnids);
1295 if (rc != LUSTRE_CFG_RC_NO_ERR) {
1306 lustre_lnet_ioctl_config_ni(struct list_head *intf_list,
1307 struct lnet_ioctl_config_lnd_tunables *tunables,
1308 struct cfs_expr_list *global_cpts,
1309 lnet_nid_t *nids, char *err_str)
1312 struct lnet_ioctl_config_ni *conf;
1313 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1314 int rc = LUSTRE_CFG_RC_NO_ERR, i = 0;
1317 struct lnet_dlc_intf_descr *intf_descr;
1319 struct cfs_expr_list *cpt_expr;
1321 list_for_each_entry(intf_descr, intf_list,
1323 if (tunables != NULL)
1324 len = sizeof(struct lnet_ioctl_config_ni) +
1325 sizeof(struct lnet_ioctl_config_lnd_tunables);
1327 len = sizeof(struct lnet_ioctl_config_ni);
1329 data = calloc(1, len);
1331 return LUSTRE_CFG_RC_OUT_OF_MEM;
1332 conf = (struct lnet_ioctl_config_ni*) data;
1333 if (tunables != NULL)
1334 tun = (struct lnet_ioctl_config_lnd_tunables*)
1337 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1338 conf->lic_cfg_hdr.ioc_len = len;
1339 conf->lic_nid = nids[i];
1340 strncpy(conf->lic_ni_intf[0], intf_descr->intf_name,
1343 if (intf_descr->cpt_expr != NULL)
1344 cpt_expr = intf_descr->cpt_expr;
1345 else if (global_cpts != NULL)
1346 cpt_expr = global_cpts;
1350 if (cpt_expr != NULL) {
1351 count = cfs_expr_list_values(cpt_expr,
1352 LNET_MAX_SHOW_NUM_CPT,
1355 memcpy(conf->lic_cpts, cpt_array,
1356 sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1365 conf->lic_ncpts = count;
1367 if (tunables != NULL)
1368 memcpy(tun, tunables, sizeof(*tunables));
1370 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1375 "\"cannot add network: %s\"", strerror(errno));
1383 return LUSTRE_CFG_RC_NO_ERR;
1387 lustre_lnet_config_ip2nets(struct lustre_lnet_ip2nets *ip2nets,
1388 struct lnet_ioctl_config_lnd_tunables *tunables,
1389 struct cfs_expr_list *global_cpts,
1390 int seq_no, struct cYAML **err_rc)
1392 lnet_nid_t *nids = NULL;
1395 char err_str[LNET_MAX_STR_LEN];
1397 snprintf(err_str, sizeof(err_str), "\"success\"");
1402 "\"incomplete ip2nets information\"");
1403 rc = LUSTRE_CFG_RC_BAD_PARAM;
1408 * call below function to resolve the rules into a list of nids.
1409 * The memory is allocated in that function then freed here when
1410 * it's no longer needed.
1412 rc = lustre_lnet_resolve_ip2nets_rule(ip2nets, &nids, &nnids);
1413 if (rc != LUSTRE_CFG_RC_NO_ERR && rc != LUSTRE_CFG_RC_MATCH) {
1416 "\"cannot resolve ip2nets rule\"");
1420 if (list_empty(&ip2nets->ip2nets_net.nw_intflist)) {
1421 snprintf(err_str, sizeof(err_str),
1422 "\"no interfaces match ip2nets rules\"");
1426 rc = lustre_lnet_ioctl_config_ni(&ip2nets->ip2nets_net.nw_intflist,
1427 tunables, global_cpts, nids,
1434 cYAML_build_error(rc, seq_no, ADD_CMD, "ip2nets", err_str, err_rc);
1438 int lustre_lnet_config_ni(struct lnet_dlc_network_descr *nw_descr,
1439 struct cfs_expr_list *global_cpts,
1441 struct lnet_ioctl_config_lnd_tunables *tunables,
1442 int seq_no, struct cYAML **err_rc)
1445 struct lnet_ioctl_config_ni *conf;
1446 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1447 char buf[LNET_MAX_STR_LEN];
1448 int rc = LUSTRE_CFG_RC_NO_ERR;
1449 char err_str[LNET_MAX_STR_LEN];
1450 lnet_nid_t *nids = NULL;
1454 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1457 snprintf(err_str, sizeof(err_str), "\"success\"");
1459 if (ip2net == NULL && nw_descr == NULL) {
1462 "\"mandatory parameters not specified.\"");
1463 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1467 if (ip2net != NULL && strlen(ip2net) >= sizeof(buf)) {
1470 "\"ip2net string too long %d\"",
1471 (int)strlen(ip2net));
1472 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1476 if (ip2net != NULL) {
1477 if (tunables != NULL)
1478 len = sizeof(struct lnet_ioctl_config_ni) +
1479 sizeof(struct lnet_ioctl_config_lnd_tunables);
1481 len = sizeof(struct lnet_ioctl_config_ni);
1482 data = calloc(1, len);
1484 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1487 conf = (struct lnet_ioctl_config_ni*) data;
1488 if (tunables != NULL)
1489 tun = (struct lnet_ioctl_config_lnd_tunables*)
1490 (data + sizeof(*conf));
1492 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1493 conf->lic_cfg_hdr.ioc_len = len;
1494 strncpy(conf->lic_legacy_ip2nets, ip2net,
1497 if (global_cpts != NULL) {
1498 count = cfs_expr_list_values(global_cpts,
1499 LNET_MAX_SHOW_NUM_CPT,
1502 memcpy(conf->lic_cpts, cpt_array,
1503 sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1512 conf->lic_ncpts = count;
1514 if (tunables != NULL)
1515 memcpy(tun, tunables, sizeof(*tunables));
1517 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1522 "\"cannot add network: %s\"", strerror(errno));
1529 if (LNET_NETTYP(nw_descr->nw_id) == LOLND) {
1530 rc = LUSTRE_CFG_RC_NO_ERR;
1534 if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1537 "\"cannot parse net '%s'\"",
1538 libcfs_net2str(nw_descr->nw_id));
1539 rc = LUSTRE_CFG_RC_BAD_PARAM;
1543 if (list_empty(&nw_descr->nw_intflist)) {
1546 "\"no interface name provided\"");
1547 rc = LUSTRE_CFG_RC_BAD_PARAM;
1551 rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids);
1553 snprintf(err_str, sizeof(err_str),
1554 "\"bad parameter\"");
1555 rc = LUSTRE_CFG_RC_BAD_PARAM;
1559 rc = lustre_lnet_ioctl_config_ni(&nw_descr->nw_intflist,
1560 tunables, global_cpts, nids,
1564 if (nw_descr != NULL) {
1565 list_for_each_entry_safe(intf_descr, tmp,
1566 &nw_descr->nw_intflist,
1568 list_del(&intf_descr->intf_on_network);
1569 free_intf_descr(intf_descr);
1573 cYAML_build_error(rc, seq_no, ADD_CMD, "net", err_str, err_rc);
1584 int lustre_lnet_del_ni(struct lnet_dlc_network_descr *nw_descr,
1585 int seq_no, struct cYAML **err_rc)
1587 struct lnet_ioctl_config_ni data;
1588 int rc = LUSTRE_CFG_RC_NO_ERR, i;
1589 char err_str[LNET_MAX_STR_LEN];
1590 lnet_nid_t *nids = NULL;
1592 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1594 snprintf(err_str, sizeof(err_str), "\"success\"");
1596 if (nw_descr == NULL) {
1599 "\"missing mandatory parameter\"");
1600 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1604 if (LNET_NETTYP(nw_descr->nw_id) == LOLND)
1605 return LUSTRE_CFG_RC_NO_ERR;
1607 if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1610 "\"cannot parse net '%s'\"",
1611 libcfs_net2str(nw_descr->nw_id));
1612 rc = LUSTRE_CFG_RC_BAD_PARAM;
1616 rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids);
1618 snprintf(err_str, sizeof(err_str),
1619 "\"bad parameter\"");
1620 rc = LUSTRE_CFG_RC_BAD_PARAM;
1625 * no interfaces just the nw_id is specified
1628 nids = calloc(1, sizeof(*nids));
1630 snprintf(err_str, sizeof(err_str),
1631 "\"out of memory\"");
1632 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1635 nids[0] = LNET_MKNID(nw_descr->nw_id, 0);
1639 for (i = 0; i < nnids; i++) {
1640 LIBCFS_IOC_INIT_V2(data, lic_cfg_hdr);
1641 data.lic_nid = nids[i];
1643 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_LOCAL_NI, &data);
1648 "\"cannot del network: %s\"", strerror(errno));
1652 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
1654 list_del(&intf_descr->intf_on_network);
1655 free_intf_descr(intf_descr);
1659 cYAML_build_error(rc, seq_no, DEL_CMD, "net", err_str, err_rc);
1667 int lustre_lnet_show_net(char *nw, int detail, int seq_no,
1668 struct cYAML **show_rc, struct cYAML **err_rc)
1671 struct lnet_ioctl_config_ni *ni_data;
1672 struct lnet_ioctl_config_lnd_tunables *lnd;
1673 struct lnet_ioctl_element_stats *stats;
1674 __u32 net = LNET_NIDNET(LNET_NID_ANY);
1675 __u32 prev_net = LNET_NIDNET(LNET_NID_ANY);
1676 int rc = LUSTRE_CFG_RC_OUT_OF_MEM, i, j;
1678 struct cYAML *root = NULL, *tunables = NULL,
1679 *net_node = NULL, *interfaces = NULL,
1680 *item = NULL, *first_seq = NULL,
1681 *tmp = NULL, *statistics = NULL;
1682 int str_buf_len = LNET_MAX_SHOW_NUM_CPT * 2;
1683 char str_buf[str_buf_len];
1685 char err_str[LNET_MAX_STR_LEN];
1686 bool exist = false, new_net = true;
1688 size_t buf_size = sizeof(*ni_data) + sizeof(*lnd) + sizeof(*stats);
1690 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1692 buf = calloc(1, buf_size);
1696 ni_data = (struct lnet_ioctl_config_ni *)buf;
1699 net = libcfs_str2net(nw);
1700 if (net == LNET_NIDNET(LNET_NID_ANY)) {
1703 "\"cannot parse net '%s'\"", nw);
1704 rc = LUSTRE_CFG_RC_BAD_PARAM;
1709 root = cYAML_create_object(NULL, NULL);
1713 net_node = cYAML_create_seq(root, "net");
1714 if (net_node == NULL)
1721 memset(buf, 0, buf_size);
1723 LIBCFS_IOC_INIT_V2(*ni_data, lic_cfg_hdr);
1725 * set the ioc_len to the proper value since INIT assumes
1728 ni_data->lic_cfg_hdr.ioc_len = buf_size;
1729 ni_data->lic_idx = i;
1731 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LOCAL_NI, ni_data);
1737 rc_net = LNET_NIDNET(ni_data->lic_nid);
1739 /* filter on provided data */
1740 if (net != LNET_NIDNET(LNET_NID_ANY) &&
1744 /* default rc to -1 in case we hit the goto */
1748 stats = (struct lnet_ioctl_element_stats *)ni_data->lic_bulk;
1749 lnd = (struct lnet_ioctl_config_lnd_tunables *)
1750 (ni_data->lic_bulk + sizeof(*stats));
1752 if (rc_net != prev_net) {
1759 if (!cYAML_create_string(net_node, "net type",
1760 libcfs_net2str(rc_net)))
1763 tmp = cYAML_create_seq(net_node, "local NI(s)");
1769 /* create the tree to be printed. */
1770 item = cYAML_create_seq_item(tmp);
1774 if (first_seq == NULL)
1777 if (cYAML_create_string(item, "nid",
1778 libcfs_nid2str(ni_data->lic_nid)) == NULL)
1781 if (cYAML_create_string(item,
1783 (ni_data->lic_status ==
1784 LNET_NI_STATUS_UP) ?
1785 "up" : "down") == NULL)
1788 /* don't add interfaces unless there is at least one
1790 if (strlen(ni_data->lic_ni_intf[0]) > 0) {
1791 interfaces = cYAML_create_object(item, "interfaces");
1792 if (interfaces == NULL)
1795 for (j = 0; j < LNET_NUM_INTERFACES; j++) {
1796 if (strlen(ni_data->lic_ni_intf[j]) > 0) {
1798 sizeof(str_buf), "%d", j);
1799 if (cYAML_create_string(interfaces,
1801 ni_data->lic_ni_intf[j]) ==
1811 statistics = cYAML_create_object(item, "statistics");
1812 if (statistics == NULL)
1815 if (cYAML_create_number(statistics, "send_count",
1816 stats->iel_send_count)
1820 if (cYAML_create_number(statistics, "recv_count",
1821 stats->iel_recv_count)
1825 if (cYAML_create_number(statistics, "drop_count",
1826 stats->iel_drop_count)
1830 tunables = cYAML_create_object(item, "tunables");
1834 rc = lustre_net_show_tunables(tunables, &lnd->lt_cmn);
1835 if (rc != LUSTRE_CFG_RC_NO_ERR)
1838 tunables = cYAML_create_object(item, "lnd tunables");
1839 if (tunables == NULL)
1842 rc = lustre_ni_show_tunables(tunables, LNET_NETTYP(rc_net),
1844 if (rc != LUSTRE_CFG_RC_NO_ERR)
1847 if (cYAML_create_number(item, "tcp bonding",
1848 ni_data->lic_tcp_bonding)
1852 if (cYAML_create_number(item, "dev cpt",
1853 ni_data->lic_dev_cpt) == NULL)
1856 /* out put the CPTs in the format: "[x,x,x,...]" */
1857 limit = str_buf + str_buf_len - 3;
1858 pos += snprintf(pos, limit - pos, "\"[");
1859 for (j = 0 ; ni_data->lic_ncpts >= 1 &&
1860 j < ni_data->lic_ncpts &&
1862 pos += snprintf(pos, limit - pos,
1863 "%d", ni_data->lic_cpts[j]);
1864 if ((j + 1) < ni_data->lic_ncpts)
1865 pos += snprintf(pos, limit - pos, ",");
1867 pos += snprintf(pos, 3, "]\"");
1869 if (ni_data->lic_ncpts >= 1 &&
1870 cYAML_create_string(item, "CPT",
1876 /* Print out the net information only if show_rc is not provided */
1877 if (show_rc == NULL)
1878 cYAML_print_tree(root);
1880 if (l_errno != ENOENT) {
1883 "\"cannot get networks: %s\"",
1888 rc = LUSTRE_CFG_RC_NO_ERR;
1890 snprintf(err_str, sizeof(err_str), "\"success\"");
1892 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
1893 cYAML_free_tree(root);
1894 } else if (show_rc != NULL && *show_rc != NULL) {
1895 struct cYAML *show_node;
1896 /* find the net node, if one doesn't exist
1897 * then insert one. Otherwise add to the one there
1899 show_node = cYAML_get_object_item(*show_rc, "net");
1900 if (show_node != NULL && cYAML_is_sequence(show_node)) {
1901 cYAML_insert_child(show_node, first_seq);
1904 } else if (show_node == NULL) {
1905 cYAML_insert_sibling((*show_rc)->cy_child,
1909 cYAML_free_tree(root);
1915 cYAML_build_error(rc, seq_no, SHOW_CMD, "net", err_str, err_rc);
1920 int lustre_lnet_enable_routing(int enable, int seq_no, struct cYAML **err_rc)
1922 struct lnet_ioctl_config_data data;
1923 int rc = LUSTRE_CFG_RC_NO_ERR;
1924 char err_str[LNET_MAX_STR_LEN];
1926 snprintf(err_str, sizeof(err_str), "\"success\"");
1928 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1929 data.cfg_config_u.cfg_buffers.buf_enable = (enable) ? 1 : 0;
1931 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIG_RTR, &data);
1936 "\"cannot %s routing %s\"",
1937 (enable) ? "enable" : "disable", strerror(errno));
1942 cYAML_build_error(rc, seq_no,
1943 (enable) ? ADD_CMD : DEL_CMD,
1944 "routing", err_str, err_rc);
1949 int lustre_lnet_config_numa_range(int range, int seq_no, struct cYAML **err_rc)
1951 struct lnet_ioctl_numa_range data;
1952 int rc = LUSTRE_CFG_RC_NO_ERR;
1953 char err_str[LNET_MAX_STR_LEN];
1955 snprintf(err_str, sizeof(err_str), "\"success\"");
1960 "\"range must be >= 0\"");
1961 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1965 LIBCFS_IOC_INIT_V2(data, nr_hdr);
1966 data.nr_range = range;
1968 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_SET_NUMA_RANGE, &data);
1973 "\"cannot configure buffers: %s\"", strerror(errno));
1978 cYAML_build_error(rc, seq_no, ADD_CMD, "numa_range", err_str, err_rc);
1983 int lustre_lnet_config_buffers(int tiny, int small, int large, int seq_no,
1984 struct cYAML **err_rc)
1986 struct lnet_ioctl_config_data data;
1987 int rc = LUSTRE_CFG_RC_NO_ERR;
1988 char err_str[LNET_MAX_STR_LEN];
1990 snprintf(err_str, sizeof(err_str), "\"success\"");
1992 /* -1 indicates to ignore changes to this field */
1993 if (tiny < -1 || small < -1 || large < -1) {
1996 "\"tiny, small and large must be >= 0\"");
1997 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
2001 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2002 data.cfg_config_u.cfg_buffers.buf_tiny = tiny;
2003 data.cfg_config_u.cfg_buffers.buf_small = small;
2004 data.cfg_config_u.cfg_buffers.buf_large = large;
2006 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_BUF, &data);
2011 "\"cannot configure buffers: %s\"", strerror(errno));
2016 cYAML_build_error(rc, seq_no, ADD_CMD, "buf", err_str, err_rc);
2021 int lustre_lnet_show_routing(int seq_no, struct cYAML **show_rc,
2022 struct cYAML **err_rc)
2024 struct lnet_ioctl_config_data *data;
2025 struct lnet_ioctl_pool_cfg *pool_cfg = NULL;
2026 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2029 char *pools[LNET_NRBPOOLS] = {"tiny", "small", "large"};
2030 int buf_count[LNET_NRBPOOLS] = {0};
2031 struct cYAML *root = NULL, *pools_node = NULL,
2032 *type_node = NULL, *item = NULL, *cpt = NULL,
2033 *first_seq = NULL, *buffers = NULL;
2035 char err_str[LNET_MAX_STR_LEN];
2036 char node_name[LNET_MAX_STR_LEN];
2039 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2041 buf = calloc(1, sizeof(*data) + sizeof(*pool_cfg));
2045 data = (struct lnet_ioctl_config_data *)buf;
2047 root = cYAML_create_object(NULL, NULL);
2051 pools_node = cYAML_create_seq(root, "routing");
2052 if (pools_node == NULL)
2056 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
2057 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
2058 sizeof(struct lnet_ioctl_pool_cfg);
2059 data->cfg_count = i;
2061 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_BUF, data);
2069 pool_cfg = (struct lnet_ioctl_pool_cfg *)data->cfg_bulk;
2071 snprintf(node_name, sizeof(node_name), "cpt[%d]", i);
2072 item = cYAML_create_seq_item(pools_node);
2076 if (first_seq == NULL)
2079 cpt = cYAML_create_object(item, node_name);
2083 /* create the tree and print */
2084 for (j = 0; j < LNET_NRBPOOLS; j++) {
2085 type_node = cYAML_create_object(cpt, pools[j]);
2086 if (type_node == NULL)
2088 if (cYAML_create_number(type_node, "npages",
2089 pool_cfg->pl_pools[j].pl_npages)
2092 if (cYAML_create_number(type_node, "nbuffers",
2093 pool_cfg->pl_pools[j].
2094 pl_nbuffers) == NULL)
2096 if (cYAML_create_number(type_node, "credits",
2097 pool_cfg->pl_pools[j].
2098 pl_credits) == NULL)
2100 if (cYAML_create_number(type_node, "mincredits",
2101 pool_cfg->pl_pools[j].
2102 pl_mincredits) == NULL)
2104 /* keep track of the total count for each of the
2105 * tiny, small and large buffers */
2106 buf_count[j] += pool_cfg->pl_pools[j].pl_nbuffers;
2110 if (pool_cfg != NULL) {
2111 item = cYAML_create_seq_item(pools_node);
2115 if (cYAML_create_number(item, "enable", pool_cfg->pl_routing) ==
2120 /* create a buffers entry in the show. This is necessary so that
2121 * if the YAML output is used to configure a node, the buffer
2122 * configuration takes hold */
2123 buffers = cYAML_create_object(root, "buffers");
2124 if (buffers == NULL)
2127 for (i = 0; i < LNET_NRBPOOLS; i++) {
2128 if (cYAML_create_number(buffers, pools[i], buf_count[i]) == NULL)
2132 if (show_rc == NULL)
2133 cYAML_print_tree(root);
2135 if (l_errno != ENOENT) {
2138 "\"cannot get routing information: %s\"",
2143 rc = LUSTRE_CFG_RC_NO_ERR;
2145 snprintf(err_str, sizeof(err_str), "\"success\"");
2146 rc = LUSTRE_CFG_RC_NO_ERR;
2150 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2151 cYAML_free_tree(root);
2152 } else if (show_rc != NULL && *show_rc != NULL) {
2153 struct cYAML *routing_node;
2154 /* there should exist only one routing block and one
2155 * buffers block. If there already exists a previous one
2156 * then don't add another */
2157 routing_node = cYAML_get_object_item(*show_rc, "routing");
2158 if (routing_node == NULL) {
2159 cYAML_insert_sibling((*show_rc)->cy_child,
2163 cYAML_free_tree(root);
2169 cYAML_build_error(rc, seq_no, SHOW_CMD, "routing", err_str, err_rc);
2174 int lustre_lnet_show_peer(char *knid, int detail, int seq_no,
2175 struct cYAML **show_rc, struct cYAML **err_rc)
2178 * TODO: This function is changing in a future patch to accommodate
2179 * PEER_LIST and proper filtering on any nid of the peer
2181 struct lnet_ioctl_peer_cfg peer_info;
2182 struct lnet_peer_ni_credit_info *lpni_cri;
2183 struct lnet_ioctl_element_stats *lpni_stats;
2184 int rc = LUSTRE_CFG_RC_OUT_OF_MEM, ncpt = 0, i = 0, j = 0;
2186 struct cYAML *root = NULL, *peer = NULL, *peer_ni = NULL,
2187 *first_seq = NULL, *peer_root = NULL, *tmp = NULL;
2188 char err_str[LNET_MAX_STR_LEN];
2189 lnet_nid_t prev_primary_nid = LNET_NID_ANY, primary_nid = LNET_NID_ANY;
2190 int data_size = sizeof(*lpni_cri) + sizeof(*lpni_stats);
2191 char *data = malloc(data_size);
2192 bool new_peer = true;
2194 snprintf(err_str, sizeof(err_str),
2195 "\"out of memory\"");
2200 /* create struct cYAML root object */
2201 root = cYAML_create_object(NULL, NULL);
2205 peer_root = cYAML_create_seq(root, "peer");
2206 if (peer_root == NULL)
2210 primary_nid = libcfs_str2nid(knid);
2214 memset(data, 0, data_size);
2215 memset(&peer_info, 0, sizeof(peer_info));
2216 LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2217 peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2218 peer_info.prcfg_count = i;
2219 peer_info.prcfg_bulk = (void *)data;
2220 peer_info.prcfg_size = data_size;
2222 rc = l_ioctl(LNET_DEV_ID,
2223 IOC_LIBCFS_GET_PEER_NI, &peer_info);
2229 if (primary_nid != LNET_NID_ANY &&
2230 primary_nid != peer_info.prcfg_prim_nid)
2233 lpni_cri = peer_info.prcfg_bulk;
2234 lpni_stats = peer_info.prcfg_bulk + sizeof(*lpni_cri);
2236 peer = cYAML_create_seq_item(peer_root);
2240 if (peer_info.prcfg_prim_nid != prev_primary_nid) {
2241 prev_primary_nid = peer_info.prcfg_prim_nid;
2246 lnet_nid_t pnid = peer_info.prcfg_prim_nid;
2247 if (cYAML_create_string(peer, "primary nid",
2248 libcfs_nid2str(pnid))
2251 if (cYAML_create_string(peer, "Multi-Rail",
2252 peer_info.prcfg_mr ?
2256 tmp = cYAML_create_seq(peer, "peer ni");
2262 if (first_seq == NULL)
2265 peer_ni = cYAML_create_seq_item(tmp);
2266 if (peer_ni == NULL)
2269 if (cYAML_create_string(peer_ni, "nid",
2271 (peer_info.prcfg_cfg_nid))
2275 if (cYAML_create_string(peer_ni, "state",
2276 lpni_cri->cr_aliveness)
2283 if (cYAML_create_number(peer_ni, "max_ni_tx_credits",
2284 lpni_cri->cr_ni_peer_tx_credits)
2288 if (cYAML_create_number(peer_ni, "available_tx_credits",
2289 lpni_cri->cr_peer_tx_credits)
2293 if (cYAML_create_number(peer_ni, "min_tx_credits",
2294 lpni_cri->cr_peer_min_tx_credits)
2298 if (cYAML_create_number(peer_ni, "tx_q_num_of_buf",
2299 lpni_cri->cr_peer_tx_qnob)
2303 if (cYAML_create_number(peer_ni, "available_rtr_credits",
2304 lpni_cri->cr_peer_rtr_credits)
2308 if (cYAML_create_number(peer_ni, "min_rtr_credits",
2309 lpni_cri->cr_peer_min_rtr_credits)
2313 if (cYAML_create_number(peer_ni, "send_count",
2314 lpni_stats->iel_send_count)
2318 if (cYAML_create_number(peer_ni, "recv_count",
2319 lpni_stats->iel_recv_count)
2323 if (cYAML_create_number(peer_ni, "drop_count",
2324 lpni_stats->iel_drop_count)
2328 if (cYAML_create_number(peer_ni, "refcount",
2329 lpni_cri->cr_refcount) == NULL)
2333 if (l_errno != ENOENT) {
2336 "\"cannot get peer information: %s\"",
2345 /* print output iff show_rc is not provided */
2346 if (show_rc == NULL)
2347 cYAML_print_tree(root);
2349 snprintf(err_str, sizeof(err_str), "\"success\"");
2350 rc = LUSTRE_CFG_RC_NO_ERR;
2353 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2354 cYAML_free_tree(root);
2355 } else if (show_rc != NULL && *show_rc != NULL) {
2356 struct cYAML *show_node;
2357 /* find the peer node, if one doesn't exist then
2358 * insert one. Otherwise add to the one there
2360 show_node = cYAML_get_object_item(*show_rc,
2362 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2363 cYAML_insert_child(show_node, first_seq);
2366 } else if (show_node == NULL) {
2367 cYAML_insert_sibling((*show_rc)->cy_child,
2371 cYAML_free_tree(root);
2377 cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
2383 int lustre_lnet_show_numa_range(int seq_no, struct cYAML **show_rc,
2384 struct cYAML **err_rc)
2386 struct lnet_ioctl_numa_range data;
2389 char err_str[LNET_MAX_STR_LEN];
2390 struct cYAML *root = NULL, *range = NULL;
2392 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2394 LIBCFS_IOC_INIT_V2(data, nr_hdr);
2396 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NUMA_RANGE, &data);
2401 "\"cannot get numa range: %s\"",
2407 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2409 root = cYAML_create_object(NULL, NULL);
2413 range = cYAML_create_object(root, "numa");
2417 if (cYAML_create_number(range, "range",
2418 data.nr_range) == NULL)
2421 if (show_rc == NULL)
2422 cYAML_print_tree(root);
2424 snprintf(err_str, sizeof(err_str), "\"success\"");
2425 rc = LUSTRE_CFG_RC_NO_ERR;
2427 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2428 cYAML_free_tree(root);
2429 } else if (show_rc != NULL && *show_rc != NULL) {
2430 cYAML_insert_sibling((*show_rc)->cy_child,
2437 cYAML_build_error(rc, seq_no, SHOW_CMD, "numa", err_str, err_rc);
2442 int lustre_lnet_show_stats(int seq_no, struct cYAML **show_rc,
2443 struct cYAML **err_rc)
2445 struct lnet_ioctl_lnet_stats data;
2448 char err_str[LNET_MAX_STR_LEN];
2449 struct cYAML *root = NULL, *stats = NULL;
2451 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2453 LIBCFS_IOC_INIT_V2(data, st_hdr);
2455 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LNET_STATS, &data);
2460 "\"cannot get lnet statistics: %s\"",
2466 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2468 root = cYAML_create_object(NULL, NULL);
2472 stats = cYAML_create_object(root, "statistics");
2476 if (cYAML_create_number(stats, "msgs_alloc",
2477 data.st_cntrs.msgs_alloc) == NULL)
2480 if (cYAML_create_number(stats, "msgs_max",
2481 data.st_cntrs.msgs_max) == NULL)
2484 if (cYAML_create_number(stats, "errors",
2485 data.st_cntrs.errors) == NULL)
2488 if (cYAML_create_number(stats, "send_count",
2489 data.st_cntrs.send_count) == NULL)
2492 if (cYAML_create_number(stats, "recv_count",
2493 data.st_cntrs.recv_count) == NULL)
2496 if (cYAML_create_number(stats, "route_count",
2497 data.st_cntrs.route_count) == NULL)
2500 if (cYAML_create_number(stats, "drop_count",
2501 data.st_cntrs.drop_count) == NULL)
2504 if (cYAML_create_number(stats, "send_length",
2505 data.st_cntrs.send_length) == NULL)
2508 if (cYAML_create_number(stats, "recv_length",
2509 data.st_cntrs.recv_length) == NULL)
2512 if (cYAML_create_number(stats, "route_length",
2513 data.st_cntrs.route_length) == NULL)
2516 if (cYAML_create_number(stats, "drop_length",
2517 data.st_cntrs.drop_length) == NULL)
2520 if (show_rc == NULL)
2521 cYAML_print_tree(root);
2523 snprintf(err_str, sizeof(err_str), "\"success\"");
2524 rc = LUSTRE_CFG_RC_NO_ERR;
2526 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2527 cYAML_free_tree(root);
2528 } else if (show_rc != NULL && *show_rc != NULL) {
2529 cYAML_insert_sibling((*show_rc)->cy_child,
2536 cYAML_build_error(rc, seq_no, SHOW_CMD, "statistics", err_str, err_rc);
2541 typedef int (*cmd_handler_t)(struct cYAML *tree,
2542 struct cYAML **show_rc,
2543 struct cYAML **err_rc);
2545 static int handle_yaml_config_route(struct cYAML *tree, struct cYAML **show_rc,
2546 struct cYAML **err_rc)
2548 struct cYAML *net, *gw, *hop, *prio, *seq_no;
2550 net = cYAML_get_object_item(tree, "net");
2551 gw = cYAML_get_object_item(tree, "gateway");
2552 hop = cYAML_get_object_item(tree, "hop");
2553 prio = cYAML_get_object_item(tree, "priority");
2554 seq_no = cYAML_get_object_item(tree, "seq_no");
2556 return lustre_lnet_config_route((net) ? net->cy_valuestring : NULL,
2557 (gw) ? gw->cy_valuestring : NULL,
2558 (hop) ? hop->cy_valueint : -1,
2559 (prio) ? prio->cy_valueint : -1,
2560 (seq_no) ? seq_no->cy_valueint : -1,
2564 static void yaml_free_string_array(char **array, int num)
2567 char **sub_array = array;
2569 for (i = 0; i < num; i++) {
2570 if (*sub_array != NULL)
2580 * 0: <intf_name>['['<expr>']']
2581 * 1: <intf_name>['['<expr>']']
2583 static int yaml_copy_intf_info(struct cYAML *intf_tree,
2584 struct lnet_dlc_network_descr *nw_descr)
2586 struct cYAML *child = NULL;
2587 int intf_num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
2588 struct lnet_dlc_intf_descr *intf_descr, *tmp;
2590 if (intf_tree == NULL || nw_descr == NULL)
2591 return LUSTRE_CFG_RC_BAD_PARAM;
2593 /* now grab all the interfaces and their cpts */
2594 child = intf_tree->cy_child;
2595 while (child != NULL) {
2596 if (child->cy_valuestring == NULL) {
2597 child = child->cy_next;
2601 if (strlen(child->cy_valuestring) >= LNET_MAX_STR_LEN)
2604 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist,
2605 child->cy_valuestring,
2606 strlen(child->cy_valuestring));
2607 if (rc != LUSTRE_CFG_RC_NO_ERR)
2611 child = child->cy_next;
2615 return LUSTRE_CFG_RC_MISSING_PARAM;
2620 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
2622 list_del(&intf_descr->intf_on_network);
2623 free_intf_descr(intf_descr);
2630 yaml_extract_cmn_tunables(struct cYAML *tree,
2631 struct lnet_ioctl_config_lnd_cmn_tunables *tunables,
2632 struct cfs_expr_list **global_cpts)
2634 struct cYAML *tun, *item, *smp;
2637 tun = cYAML_get_object_item(tree, "tunables");
2639 item = cYAML_get_object_item(tun, "peer_timeout");
2641 tunables->lct_peer_timeout = item->cy_valueint;
2642 item = cYAML_get_object_item(tun, "peer_credits");
2644 tunables->lct_peer_tx_credits = item->cy_valueint;
2645 item = cYAML_get_object_item(tun, "peer_buffer_credits");
2647 tunables->lct_peer_rtr_credits = item->cy_valueint;
2648 item = cYAML_get_object_item(tun, "credits");
2650 tunables->lct_max_tx_credits = item->cy_valueint;
2651 smp = cYAML_get_object_item(tun, "CPT");
2653 rc = cfs_expr_list_parse(smp->cy_valuestring,
2654 strlen(smp->cy_valuestring),
2655 0, UINT_MAX, global_cpts);
2657 *global_cpts = NULL;
2667 yaml_extract_tunables(struct cYAML *tree,
2668 struct lnet_ioctl_config_lnd_tunables *tunables,
2669 struct cfs_expr_list **global_cpts,
2674 rc = yaml_extract_cmn_tunables(tree, &tunables->lt_cmn,
2680 lustre_yaml_extract_lnd_tunables(tree, net_type,
2688 * - net type: <net>[<NUM>]
2690 * - nid: <ip>@<net>[<NUM>]
2693 * 0: <intf_name>['['<expr>']']
2694 * 1: <intf_name>['['<expr>']']
2696 * peer_timeout: <NUM>
2697 * peer_credits: <NUM>
2698 * peer_buffer_credits: <NUM>
2701 * peercredits_hiw: <NUM>
2702 * map_on_demand: <NUM>
2703 * concurrent_sends: <NUM>
2704 * fmr_pool_size: <NUM>
2705 * fmr_flush_trigger: <NUM>
2708 * At least one interface is required. If no interfaces are provided the
2709 * network interface can not be configured.
2711 static int handle_yaml_config_ni(struct cYAML *tree, struct cYAML **show_rc,
2712 struct cYAML **err_rc)
2714 struct cYAML *net, *intf, *seq_no, *ip2net = NULL, *local_nis = NULL,
2716 int num_entries = 0, rc;
2717 struct lnet_dlc_network_descr nw_descr;
2718 struct cfs_expr_list *global_cpts = NULL;
2719 struct lnet_ioctl_config_lnd_tunables tunables;
2722 memset(&tunables, 0, sizeof(tunables));
2724 INIT_LIST_HEAD(&nw_descr.network_on_rule);
2725 INIT_LIST_HEAD(&nw_descr.nw_intflist);
2727 ip2net = cYAML_get_object_item(tree, "ip2net");
2728 net = cYAML_get_object_item(tree, "net type");
2730 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
2732 nw_descr.nw_id = LOLND;
2735 * if neither net nor ip2nets are present, then we can not
2736 * configure the network.
2738 if (!net && !ip2net)
2739 return LUSTRE_CFG_RC_MISSING_PARAM;
2741 local_nis = cYAML_get_object_item(tree, "local NI(s)");
2742 if (local_nis == NULL)
2743 return LUSTRE_CFG_RC_MISSING_PARAM;
2745 if (!cYAML_is_sequence(local_nis))
2746 return LUSTRE_CFG_RC_BAD_PARAM;
2748 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
2749 intf = cYAML_get_object_item(item, "interfaces");
2752 num_entries = yaml_copy_intf_info(intf, &nw_descr);
2753 if (num_entries <= 0) {
2754 cYAML_build_error(num_entries, -1, "ni", "add",
2755 "bad interface list",
2757 return LUSTRE_CFG_RC_BAD_PARAM;
2761 found = yaml_extract_tunables(tree, &tunables, &global_cpts,
2762 LNET_NETTYP(nw_descr.nw_id));
2763 seq_no = cYAML_get_object_item(tree, "seq_no");
2765 rc = lustre_lnet_config_ni(&nw_descr,
2767 (ip2net) ? ip2net->cy_valuestring : NULL,
2768 (found) ? &tunables: NULL,
2769 (seq_no) ? seq_no->cy_valueint : -1,
2772 if (global_cpts != NULL)
2773 cfs_expr_list_free(global_cpts);
2780 * - net-spec: <tcp|o2ib|gni>[NUM]
2782 * 0: <intf name>['['<expr>']']
2783 * 1: <intf name>['['<expr>']']
2785 * 0: <expr.expr.expr.expr>
2786 * 1: <expr.expr.expr.expr>
2788 static int handle_yaml_config_ip2nets(struct cYAML *tree,
2789 struct cYAML **show_rc,
2790 struct cYAML **err_rc)
2792 struct cYAML *net, *ip_range, *item = NULL, *intf = NULL,
2794 struct lustre_lnet_ip2nets ip2nets;
2795 struct lustre_lnet_ip_range_descr *ip_range_descr = NULL,
2797 int rc = LUSTRE_CFG_RC_NO_ERR;
2798 struct cfs_expr_list *global_cpts = NULL;
2799 struct cfs_expr_list *el, *el_tmp;
2800 struct lnet_ioctl_config_lnd_tunables tunables;
2801 struct lnet_dlc_intf_descr *intf_descr, *intf_tmp;
2804 memset(&tunables, 0, sizeof(tunables));
2806 /* initialize all lists */
2807 INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
2808 INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
2809 INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
2811 net = cYAML_get_object_item(tree, "net-spec");
2813 return LUSTRE_CFG_RC_BAD_PARAM;
2815 if (net != NULL && net->cy_valuestring == NULL)
2816 return LUSTRE_CFG_RC_BAD_PARAM;
2818 /* assign the network id */
2819 ip2nets.ip2nets_net.nw_id = libcfs_str2net(net->cy_valuestring);
2820 if (ip2nets.ip2nets_net.nw_id == LNET_NID_ANY)
2821 return LUSTRE_CFG_RC_BAD_PARAM;
2823 seq_no = cYAML_get_object_item(tree, "seq_no");
2825 intf = cYAML_get_object_item(tree, "interfaces");
2827 rc = yaml_copy_intf_info(intf, &ip2nets.ip2nets_net);
2829 return LUSTRE_CFG_RC_BAD_PARAM;
2832 ip_range = cYAML_get_object_item(tree, "ip-range");
2833 if (ip_range != NULL) {
2834 item = ip_range->cy_child;
2835 while (item != NULL) {
2836 if (item->cy_valuestring == NULL) {
2837 item = item->cy_next;
2841 rc = lustre_lnet_add_ip_range(&ip2nets.ip2nets_ip_ranges,
2842 item->cy_valuestring);
2844 if (rc != LUSTRE_CFG_RC_NO_ERR)
2847 item = item->cy_next;
2851 found = yaml_extract_tunables(tree, &tunables, &global_cpts,
2852 LNET_NETTYP(ip2nets.ip2nets_net.nw_id));
2854 rc = lustre_lnet_config_ip2nets(&ip2nets,
2855 (found) ? &tunables : NULL,
2857 (seq_no) ? seq_no->cy_valueint : -1,
2861 * don't stop because there was no match. Continue processing the
2862 * rest of the rules. If non-match then nothing is configured
2864 if (rc == LUSTRE_CFG_RC_NO_MATCH)
2865 rc = LUSTRE_CFG_RC_NO_ERR;
2867 list_for_each_entry_safe(intf_descr, intf_tmp,
2868 &ip2nets.ip2nets_net.nw_intflist,
2870 list_del(&intf_descr->intf_on_network);
2871 free_intf_descr(intf_descr);
2874 list_for_each_entry_safe(ip_range_descr, tmp,
2875 &ip2nets.ip2nets_ip_ranges,
2877 list_del(&ip_range_descr->ipr_entry);
2878 list_for_each_entry_safe(el, el_tmp, &ip_range_descr->ipr_expr,
2880 list_del(&el->el_link);
2881 cfs_expr_list_free(el);
2883 free(ip_range_descr);
2889 static int handle_yaml_del_ni(struct cYAML *tree, struct cYAML **show_rc,
2890 struct cYAML **err_rc)
2892 struct cYAML *net = NULL, *intf = NULL, *seq_no = NULL, *item = NULL,
2894 int num_entries, rc;
2895 struct lnet_dlc_network_descr nw_descr;
2897 INIT_LIST_HEAD(&nw_descr.network_on_rule);
2898 INIT_LIST_HEAD(&nw_descr.nw_intflist);
2900 net = cYAML_get_object_item(tree, "net type");
2902 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
2904 local_nis = cYAML_get_object_item(tree, "local NI(s)");
2905 if (local_nis == NULL)
2906 return LUSTRE_CFG_RC_MISSING_PARAM;
2908 if (!cYAML_is_sequence(local_nis))
2909 return LUSTRE_CFG_RC_BAD_PARAM;
2911 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
2912 intf = cYAML_get_object_item(item, "interfaces");
2915 num_entries = yaml_copy_intf_info(intf, &nw_descr);
2916 if (num_entries <= 0) {
2917 cYAML_build_error(num_entries, -1, "ni", "add",
2918 "bad interface list",
2920 return LUSTRE_CFG_RC_BAD_PARAM;
2924 seq_no = cYAML_get_object_item(tree, "seq_no");
2926 rc = lustre_lnet_del_ni((net) ? &nw_descr : NULL,
2927 (seq_no) ? seq_no->cy_valueint : -1,
2933 static int yaml_copy_peer_nids(struct cYAML *nids_entry, char ***nidsppp,
2934 char *prim_nid, bool del)
2936 struct cYAML *child = NULL, *entry = NULL;
2938 int num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
2940 if (cYAML_is_sequence(nids_entry)) {
2941 while (cYAML_get_next_seq_item(nids_entry, &child)) {
2942 entry = cYAML_get_object_item(child, "nid");
2943 /* don't count an empty entry */
2944 if (!entry || !entry->cy_valuestring)
2948 (strcmp(entry->cy_valuestring, prim_nid)
2951 * primary nid is present in the list of
2952 * nids so that means we want to delete
2953 * the entire peer, so no need to go
2954 * further. Just delete the entire peer.
2964 return LUSTRE_CFG_RC_MISSING_PARAM;
2966 nids = calloc(sizeof(*nids), num);
2968 return LUSTRE_CFG_RC_OUT_OF_MEM;
2970 /* now grab all the nids */
2973 while (cYAML_get_next_seq_item(nids_entry, &child)) {
2974 entry = cYAML_get_object_item(child, "nid");
2975 if (!entry || !entry->cy_valuestring)
2978 nids[num] = calloc(strlen(entry->cy_valuestring) + 1, 1);
2980 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2983 strncpy(nids[num], entry->cy_valuestring,
2984 strlen(entry->cy_valuestring));
2994 yaml_free_string_array(nids, num);
2999 static int handle_yaml_config_peer(struct cYAML *tree, struct cYAML **show_rc,
3000 struct cYAML **err_rc)
3004 struct cYAML *seq_no, *prim_nid, *non_mr, *ip2nets, *peer_nis;
3005 char err_str[LNET_MAX_STR_LEN];
3007 seq_no = cYAML_get_object_item(tree, "seq_no");
3008 prim_nid = cYAML_get_object_item(tree, "primary nid");
3009 non_mr = cYAML_get_object_item(tree, "non_mr");
3010 ip2nets = cYAML_get_object_item(tree, "ip2nets");
3011 peer_nis = cYAML_get_object_item(tree, "peer ni");
3013 if (ip2nets && (prim_nid || peer_nis)) {
3014 rc = LUSTRE_CFG_RC_BAD_PARAM;
3015 snprintf(err_str, sizeof(err_str),
3016 "ip2nets can not be specified along side prim_nid"
3017 " or peer ni fields");
3018 cYAML_build_error(rc, (seq_no) ? seq_no->cy_valueint : -1,
3019 ADD_CMD, "peer", err_str, err_rc);
3023 num = yaml_copy_peer_nids((ip2nets) ? ip2nets : peer_nis, &nids,
3024 (prim_nid) ? prim_nid->cy_valuestring : NULL,
3028 snprintf(err_str, sizeof(err_str),
3029 "error copying nids from YAML block");
3030 cYAML_build_error(num, (seq_no) ? seq_no->cy_valueint : -1,
3031 ADD_CMD, "peer", err_str, err_rc);
3035 rc = lustre_lnet_config_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
3037 (non_mr) ? false : true,
3038 (ip2nets) ? true : false,
3039 (seq_no) ? seq_no->cy_valueint : -1,
3042 yaml_free_string_array(nids, num);
3046 static int handle_yaml_del_peer(struct cYAML *tree, struct cYAML **show_rc,
3047 struct cYAML **err_rc)
3051 struct cYAML *seq_no, *prim_nid, *ip2nets, *peer_nis;
3052 char err_str[LNET_MAX_STR_LEN];
3054 seq_no = cYAML_get_object_item(tree, "seq_no");
3055 prim_nid = cYAML_get_object_item(tree, "primary nid");
3056 ip2nets = cYAML_get_object_item(tree, "ip2nets");
3057 peer_nis = cYAML_get_object_item(tree, "peer ni");
3059 if (ip2nets && (prim_nid || peer_nis)) {
3060 rc = LUSTRE_CFG_RC_BAD_PARAM;
3061 snprintf(err_str, sizeof(err_str),
3062 "ip2nets can not be specified along side prim_nid"
3063 " or peer ni fields");
3064 cYAML_build_error(rc, (seq_no) ? seq_no->cy_valueint : -1,
3065 DEL_CMD, "peer", err_str, err_rc);
3069 num = yaml_copy_peer_nids((ip2nets) ? ip2nets : peer_nis , &nids,
3070 (prim_nid) ? prim_nid->cy_valuestring : NULL,
3073 snprintf(err_str, sizeof(err_str),
3074 "error copying nids from YAML block");
3075 cYAML_build_error(num, (seq_no) ? seq_no->cy_valueint : -1,
3076 ADD_CMD, "peer", err_str, err_rc);
3080 rc = lustre_lnet_del_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
3081 nids, num, (ip2nets) ? true : false,
3082 (seq_no) ? seq_no->cy_valueint : -1,
3085 yaml_free_string_array(nids, num);
3089 static int handle_yaml_config_buffers(struct cYAML *tree,
3090 struct cYAML **show_rc,
3091 struct cYAML **err_rc)
3094 struct cYAML *tiny, *small, *large, *seq_no;
3096 tiny = cYAML_get_object_item(tree, "tiny");
3097 small = cYAML_get_object_item(tree, "small");
3098 large = cYAML_get_object_item(tree, "large");
3099 seq_no = cYAML_get_object_item(tree, "seq_no");
3101 rc = lustre_lnet_config_buffers((tiny) ? tiny->cy_valueint : -1,
3102 (small) ? small->cy_valueint : -1,
3103 (large) ? large->cy_valueint : -1,
3104 (seq_no) ? seq_no->cy_valueint : -1,
3110 static int handle_yaml_config_routing(struct cYAML *tree,
3111 struct cYAML **show_rc,
3112 struct cYAML **err_rc)
3114 int rc = LUSTRE_CFG_RC_NO_ERR;
3115 struct cYAML *seq_no, *enable;
3117 seq_no = cYAML_get_object_item(tree, "seq_no");
3118 enable = cYAML_get_object_item(tree, "enable");
3121 rc = lustre_lnet_enable_routing(enable->cy_valueint,
3123 seq_no->cy_valueint : -1,
3130 static int handle_yaml_del_route(struct cYAML *tree, struct cYAML **show_rc,
3131 struct cYAML **err_rc)
3135 struct cYAML *seq_no;
3137 net = cYAML_get_object_item(tree, "net");
3138 gw = cYAML_get_object_item(tree, "gateway");
3139 seq_no = cYAML_get_object_item(tree, "seq_no");
3141 return lustre_lnet_del_route((net) ? net->cy_valuestring : NULL,
3142 (gw) ? gw->cy_valuestring : NULL,
3143 (seq_no) ? seq_no->cy_valueint : -1,
3147 static int handle_yaml_del_routing(struct cYAML *tree, struct cYAML **show_rc,
3148 struct cYAML **err_rc)
3150 struct cYAML *seq_no;
3152 seq_no = cYAML_get_object_item(tree, "seq_no");
3154 return lustre_lnet_enable_routing(0, (seq_no) ?
3155 seq_no->cy_valueint : -1,
3159 static int handle_yaml_show_route(struct cYAML *tree, struct cYAML **show_rc,
3160 struct cYAML **err_rc)
3166 struct cYAML *detail;
3167 struct cYAML *seq_no;
3169 net = cYAML_get_object_item(tree, "net");
3170 gw = cYAML_get_object_item(tree, "gateway");
3171 hop = cYAML_get_object_item(tree, "hop");
3172 prio = cYAML_get_object_item(tree, "priority");
3173 detail = cYAML_get_object_item(tree, "detail");
3174 seq_no = cYAML_get_object_item(tree, "seq_no");
3176 return lustre_lnet_show_route((net) ? net->cy_valuestring : NULL,
3177 (gw) ? gw->cy_valuestring : NULL,
3178 (hop) ? hop->cy_valueint : -1,
3179 (prio) ? prio->cy_valueint : -1,
3180 (detail) ? detail->cy_valueint : 0,
3181 (seq_no) ? seq_no->cy_valueint : -1,
3186 static int handle_yaml_show_net(struct cYAML *tree, struct cYAML **show_rc,
3187 struct cYAML **err_rc)
3189 struct cYAML *net, *detail, *seq_no;
3191 net = cYAML_get_object_item(tree, "net");
3192 detail = cYAML_get_object_item(tree, "detail");
3193 seq_no = cYAML_get_object_item(tree, "seq_no");
3195 return lustre_lnet_show_net((net) ? net->cy_valuestring : NULL,
3196 (detail) ? detail->cy_valueint : 0,
3197 (seq_no) ? seq_no->cy_valueint : -1,
3202 static int handle_yaml_show_routing(struct cYAML *tree, struct cYAML **show_rc,
3203 struct cYAML **err_rc)
3205 struct cYAML *seq_no;
3207 seq_no = cYAML_get_object_item(tree, "seq_no");
3209 return lustre_lnet_show_routing((seq_no) ? seq_no->cy_valueint : -1,
3213 static int handle_yaml_show_peers(struct cYAML *tree, struct cYAML **show_rc,
3214 struct cYAML **err_rc)
3216 struct cYAML *seq_no, *nid, *detail;
3218 seq_no = cYAML_get_object_item(tree, "seq_no");
3219 detail = cYAML_get_object_item(tree, "detail");
3220 nid = cYAML_get_object_item(tree, "nid");
3222 return lustre_lnet_show_peer((nid) ? nid->cy_valuestring : NULL,
3223 (detail) ? detail->cy_valueint : 0,
3224 (seq_no) ? seq_no->cy_valueint : -1,
3228 static int handle_yaml_show_stats(struct cYAML *tree, struct cYAML **show_rc,
3229 struct cYAML **err_rc)
3231 struct cYAML *seq_no;
3233 seq_no = cYAML_get_object_item(tree, "seq_no");
3235 return lustre_lnet_show_stats((seq_no) ? seq_no->cy_valueint : -1,
3239 static int handle_yaml_config_numa(struct cYAML *tree, struct cYAML **show_rc,
3240 struct cYAML **err_rc)
3242 struct cYAML *seq_no, *range;
3244 seq_no = cYAML_get_object_item(tree, "seq_no");
3245 range = cYAML_get_object_item(tree, "range");
3247 return lustre_lnet_config_numa_range(range ? range->cy_valueint : -1,
3248 seq_no ? seq_no->cy_valueint : -1,
3252 static int handle_yaml_del_numa(struct cYAML *tree, struct cYAML **show_rc,
3253 struct cYAML **err_rc)
3255 struct cYAML *seq_no;
3257 seq_no = cYAML_get_object_item(tree, "seq_no");
3259 return lustre_lnet_config_numa_range(0, seq_no ? seq_no->cy_valueint : -1,
3263 static int handle_yaml_show_numa(struct cYAML *tree, struct cYAML **show_rc,
3264 struct cYAML **err_rc)
3266 struct cYAML *seq_no;
3268 seq_no = cYAML_get_object_item(tree, "seq_no");
3270 return lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint : -1,
3274 struct lookup_cmd_hdlr_tbl {
3279 static struct lookup_cmd_hdlr_tbl lookup_config_tbl[] = {
3280 { .name = "route", .cb = handle_yaml_config_route },
3281 { .name = "net", .cb = handle_yaml_config_ni },
3282 { .name = "ip2nets", .cb = handle_yaml_config_ip2nets },
3283 { .name = "peer", .cb = handle_yaml_config_peer },
3284 { .name = "routing", .cb = handle_yaml_config_routing },
3285 { .name = "buffers", .cb = handle_yaml_config_buffers },
3286 { .name = "numa", .cb = handle_yaml_config_numa },
3289 static struct lookup_cmd_hdlr_tbl lookup_del_tbl[] = {
3290 { .name = "route", .cb = handle_yaml_del_route },
3291 { .name = "net", .cb = handle_yaml_del_ni },
3292 { .name = "peer", .cb = handle_yaml_del_peer },
3293 { .name = "routing", .cb = handle_yaml_del_routing },
3294 { .name = "numa", .cb = handle_yaml_del_numa },
3297 static struct lookup_cmd_hdlr_tbl lookup_show_tbl[] = {
3298 { .name = "route", .cb = handle_yaml_show_route },
3299 { .name = "net", .cb = handle_yaml_show_net },
3300 { .name = "buffers", .cb = handle_yaml_show_routing },
3301 { .name = "routing", .cb = handle_yaml_show_routing },
3302 { .name = "peer", .cb = handle_yaml_show_peers },
3303 { .name = "statistics", .cb = handle_yaml_show_stats },
3304 { .name = "numa", .cb = handle_yaml_show_numa },
3307 static cmd_handler_t lookup_fn(char *key,
3308 struct lookup_cmd_hdlr_tbl *tbl)
3314 for (i = 0; tbl[i].name != NULL; i++) {
3315 if (strncmp(key, tbl[i].name, strlen(tbl[i].name)) == 0)
3322 static int lustre_yaml_cb_helper(char *f, struct lookup_cmd_hdlr_tbl *table,
3323 struct cYAML **show_rc, struct cYAML **err_rc)
3325 struct cYAML *tree, *item = NULL, *head, *child;
3327 char err_str[LNET_MAX_STR_LEN];
3328 int rc = LUSTRE_CFG_RC_NO_ERR, return_rc = LUSTRE_CFG_RC_NO_ERR;
3330 tree = cYAML_build_tree(f, NULL, 0, err_rc, false);
3332 return LUSTRE_CFG_RC_BAD_PARAM;
3334 child = tree->cy_child;
3335 while (child != NULL) {
3336 cb = lookup_fn(child->cy_string, table);
3338 snprintf(err_str, sizeof(err_str),
3339 "\"call back for '%s' not found\"",
3341 cYAML_build_error(LUSTRE_CFG_RC_BAD_PARAM, -1,
3342 "yaml", "helper", err_str, err_rc);
3346 if (cYAML_is_sequence(child)) {
3347 while ((head = cYAML_get_next_seq_item(child, &item))
3349 rc = cb(head, show_rc, err_rc);
3350 if (rc != LUSTRE_CFG_RC_NO_ERR)
3354 rc = cb(child, show_rc, err_rc);
3355 if (rc != LUSTRE_CFG_RC_NO_ERR)
3359 child = child->cy_next;
3363 cYAML_free_tree(tree);
3368 int lustre_yaml_config(char *f, struct cYAML **err_rc)
3370 return lustre_yaml_cb_helper(f, lookup_config_tbl,
3374 int lustre_yaml_del(char *f, struct cYAML **err_rc)
3376 return lustre_yaml_cb_helper(f, lookup_del_tbl,
3380 int lustre_yaml_show(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
3382 return lustre_yaml_cb_helper(f, lookup_show_tbl,