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;
732 __u32 rnet = LNET_NIDNET(LNET_NID_ANY);
733 __u32 net = LNET_NIDNET(LNET_NID_ANY);
734 char err_str[LNET_MAX_STR_LEN];
735 __u32 ip_list[MAX_NUM_IPS];
736 struct lustre_lnet_ip2nets ip2nets;
738 /* initialize all lists */
739 INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
740 INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
741 INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
743 snprintf(err_str, sizeof(err_str), "\"Success\"");
745 if (nw == NULL || gw == NULL) {
748 "\"missing mandatory parameter(s): '%s'\"",
749 (nw == NULL && gw == NULL) ? "network, gateway" :
750 (nw == NULL) ? "network" : "gateway");
751 rc = LUSTRE_CFG_RC_MISSING_PARAM;
755 rnet = libcfs_str2net(nw);
756 if (rnet == LNET_NIDNET(LNET_NID_ANY)) {
759 "\"cannot parse remote net %s\"", nw);
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 rc = lnet_expr2ips(gw, ip_list,
788 &ip2nets, &net, err_str);
789 if (rc == LUSTRE_CFG_RC_LAST_ELEM)
791 else if (rc < LUSTRE_CFG_RC_NO_ERR)
796 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
798 data.cfg_config_u.cfg_route.rtr_hop = hops;
799 data.cfg_config_u.cfg_route.rtr_priority = prio;
801 for (i = MAX_NUM_IPS - 1; i > ip_idx; i--) {
802 gateway_nid = LNET_MKNID(net, ip_list[i]);
803 if (gateway_nid == LNET_NID_ANY) {
806 "\"cannot form gateway NID: %u\"",
808 err_str[LNET_MAX_STR_LEN - 1] = '\0';
809 rc = LUSTRE_CFG_RC_BAD_PARAM;
812 data.cfg_nid = gateway_nid;
814 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
819 "\"cannot add route: %s\"", strerror(errno));
824 cYAML_build_error(rc, seq_no, ADD_CMD, "route", err_str, err_rc);
829 int lustre_lnet_del_route(char *nw, char *gw,
830 int seq_no, struct cYAML **err_rc)
832 struct lnet_ioctl_config_data data;
833 lnet_nid_t gateway_nid;
834 int rc = LUSTRE_CFG_RC_NO_ERR;
835 __u32 rnet = LNET_NIDNET(LNET_NID_ANY);
836 __u32 net = LNET_NIDNET(LNET_NID_ANY);
837 char err_str[LNET_MAX_STR_LEN];
839 __u32 ip_list[MAX_NUM_IPS];
840 struct lustre_lnet_ip2nets ip2nets;
842 /* initialize all lists */
843 INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
844 INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
845 INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
847 snprintf(err_str, sizeof(err_str), "\"Success\"");
849 if (nw == NULL || gw == NULL) {
852 "\"missing mandatory parameter(s): '%s'\"",
853 (nw == NULL && gw == NULL) ? "network, gateway" :
854 (nw == NULL) ? "network" : "gateway");
855 rc = LUSTRE_CFG_RC_MISSING_PARAM;
859 rnet = libcfs_str2net(nw);
860 if (rnet == LNET_NIDNET(LNET_NID_ANY)) {
863 "\"cannot parse remote net '%s'\"", nw);
864 rc = LUSTRE_CFG_RC_BAD_PARAM;
868 rc = lnet_expr2ips(gw, ip_list,
869 &ip2nets, &net, err_str);
870 if (rc == LUSTRE_CFG_RC_LAST_ELEM)
872 else if (rc < LUSTRE_CFG_RC_NO_ERR)
877 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
880 for (i = MAX_NUM_IPS - 1; i > ip_idx; i--) {
881 gateway_nid = LNET_MKNID(net, ip_list[i]);
882 if (gateway_nid == LNET_NID_ANY) {
885 "\"cannot form gateway NID: %u\"",
887 err_str[LNET_MAX_STR_LEN - 1] = '\0';
888 rc = LUSTRE_CFG_RC_BAD_PARAM;
891 data.cfg_nid = gateway_nid;
893 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
898 "\"cannot delete route: %s\"", strerror(errno));
903 cYAML_build_error(rc, seq_no, DEL_CMD, "route", err_str, err_rc);
908 int lustre_lnet_show_route(char *nw, char *gw, int hops, int prio, int detail,
909 int seq_no, struct cYAML **show_rc,
910 struct cYAML **err_rc)
912 struct lnet_ioctl_config_data data;
913 lnet_nid_t gateway_nid;
914 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
916 __u32 net = LNET_NIDNET(LNET_NID_ANY);
918 struct cYAML *root = NULL, *route = NULL, *item = NULL;
919 struct cYAML *first_seq = NULL;
920 char err_str[LNET_MAX_STR_LEN];
923 snprintf(err_str, sizeof(err_str),
924 "\"out of memory\"");
927 net = libcfs_str2net(nw);
928 if (net == LNET_NIDNET(LNET_NID_ANY)) {
931 "\"cannot parse net '%s'\"", nw);
932 rc = LUSTRE_CFG_RC_BAD_PARAM;
937 /* show all routes without filtering on net */
938 net = LNET_NIDNET(LNET_NID_ANY);
942 gateway_nid = libcfs_str2nid(gw);
943 if (gateway_nid == LNET_NID_ANY) {
946 "\"cannot parse gateway NID '%s'\"", gw);
947 rc = LUSTRE_CFG_RC_BAD_PARAM;
951 /* show all routes with out filtering on gateway */
952 gateway_nid = LNET_NID_ANY;
954 if ((hops < 1 && hops != -1) || hops > 255) {
957 "\"invalid hop count %d, must be between 0 and 256\"",
959 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
963 /* create struct cYAML root object */
964 root = cYAML_create_object(NULL, NULL);
968 route = cYAML_create_seq(root, "route");
973 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
976 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
982 /* filter on provided data */
983 if (net != LNET_NIDNET(LNET_NID_ANY) &&
987 if (gateway_nid != LNET_NID_ANY &&
988 gateway_nid != data.cfg_nid)
992 hops != data.cfg_config_u.cfg_route.rtr_hop)
996 prio != data.cfg_config_u.cfg_route.rtr_priority)
999 /* default rc to -1 incase we hit the goto */
1003 item = cYAML_create_seq_item(route);
1007 if (first_seq == NULL)
1010 if (cYAML_create_string(item, "net",
1011 libcfs_net2str(data.cfg_net)) == NULL)
1014 if (cYAML_create_string(item, "gateway",
1015 libcfs_nid2str(data.cfg_nid)) == NULL)
1019 if (cYAML_create_number(item, "hop",
1020 (int) data.cfg_config_u.
1021 cfg_route.rtr_hop) ==
1025 if (cYAML_create_number(item, "priority",
1027 cfg_route.rtr_priority) == NULL)
1030 if (cYAML_create_string(item, "state",
1031 data.cfg_config_u.cfg_route.
1033 "up" : "down") == NULL)
1038 /* print output iff show_rc is not provided */
1039 if (show_rc == NULL)
1040 cYAML_print_tree(root);
1042 if (l_errno != ENOENT) {
1045 "\"cannot get routes: %s\"",
1050 rc = LUSTRE_CFG_RC_NO_ERR;
1052 snprintf(err_str, sizeof(err_str), "\"success\"");
1054 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
1055 cYAML_free_tree(root);
1056 } else if (show_rc != NULL && *show_rc != NULL) {
1057 struct cYAML *show_node;
1058 /* find the route node, if one doesn't exist then
1059 * insert one. Otherwise add to the one there
1061 show_node = cYAML_get_object_item(*show_rc, "route");
1062 if (show_node != NULL && cYAML_is_sequence(show_node)) {
1063 cYAML_insert_child(show_node, first_seq);
1066 } else if (show_node == NULL) {
1067 cYAML_insert_sibling((*show_rc)->cy_child,
1071 cYAML_free_tree(root);
1077 cYAML_build_error(rc, seq_no, SHOW_CMD, "route", err_str, err_rc);
1082 static int socket_intf_query(int request, char *intf,
1088 if (strlen(intf) >= IFNAMSIZ || ifr == NULL)
1089 return LUSTRE_CFG_RC_BAD_PARAM;
1091 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
1093 return LUSTRE_CFG_RC_BAD_PARAM;
1095 strcpy(ifr->ifr_name, intf);
1096 rc = ioctl(sockfd, request, ifr);
1098 rc = LUSTRE_CFG_RC_BAD_PARAM;
1106 * for each interface in the array of interfaces find the IP address of
1107 * that interface, create its nid and add it to an array of NIDs.
1108 * Stop if any of the interfaces is down
1110 static int lustre_lnet_intf2nids(struct lnet_dlc_network_descr *nw,
1111 lnet_nid_t **nids, __u32 *nnids)
1113 int i = 0, count = 0, rc;
1116 struct lnet_dlc_intf_descr *intf;
1118 if (nw == NULL || nids == NULL)
1119 return LUSTRE_CFG_RC_BAD_PARAM;
1121 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network)
1124 *nids = calloc(count, sizeof(lnet_nid_t));
1126 return LUSTRE_CFG_RC_OUT_OF_MEM;
1128 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network) {
1129 memset(&ifr, 0, sizeof(ifr));
1130 rc = socket_intf_query(SIOCGIFFLAGS, intf->intf_name, &ifr);
1134 if ((ifr.ifr_flags & IFF_UP) == 0) {
1135 rc = LUSTRE_CFG_RC_BAD_PARAM;
1139 memset(&ifr, 0, sizeof(ifr));
1140 rc = socket_intf_query(SIOCGIFADDR, intf->intf_name, &ifr);
1144 ip = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
1146 (*nids)[i] = LNET_MKNID(nw->nw_id, ip);
1161 * called repeatedly until a match or no more ip range
1163 * ip_range expression
1164 * interface list with all the interface names.
1165 * all the interfaces in the system.
1167 * try to match the ip_range expr to one of the interfaces' IPs in
1168 * the system. If we hit a patch for an interface. Check if that
1169 * interface name is in the list.
1171 * If there are more than one interface in the list, then make sure
1172 * that the IPs for all of these interfaces match the ip ranges
1175 * for each interface in intf_list
1176 * look up the intf name in ifa
1177 * if not there then no match
1178 * check ip obtained from ifa against a match to any of the
1180 * If no match, then fail
1182 * The result is that all the interfaces have to match.
1184 int lustre_lnet_match_ip_to_intf(struct ifaddrs *ifa,
1185 struct list_head *intf_list,
1186 struct list_head *ip_ranges)
1190 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1191 struct ifaddrs *ifaddr = ifa;
1192 struct lustre_lnet_ip_range_descr *ip_range;
1196 * if there are no explicit interfaces, and no ip ranges, then
1197 * configure the first tcp interface we encounter.
1199 if (list_empty(intf_list) && list_empty(ip_ranges)) {
1200 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1201 if (ifaddr->ifa_addr == NULL)
1204 if ((ifaddr->ifa_flags & IFF_UP) == 0)
1207 family = ifaddr->ifa_addr->sa_family;
1208 if (family == AF_INET &&
1209 strcmp(ifaddr->ifa_name, "lo") != 0) {
1210 rc = lustre_lnet_add_intf_descr
1211 (intf_list, ifaddr->ifa_name,
1212 strlen(ifaddr->ifa_name));
1214 if (rc != LUSTRE_CFG_RC_NO_ERR)
1217 return LUSTRE_CFG_RC_MATCH;
1220 return LUSTRE_CFG_RC_NO_MATCH;
1224 * First interface which matches an IP pattern will be used
1226 if (list_empty(intf_list)) {
1228 * no interfaces provided in the rule, but an ip range is
1229 * provided, so try and match an interface to the ip
1232 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1233 if (ifaddr->ifa_addr == NULL)
1236 if ((ifaddr->ifa_flags & IFF_UP) == 0)
1239 family = ifaddr->ifa_addr->sa_family;
1240 if (family == AF_INET) {
1241 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->
1244 list_for_each_entry(ip_range, ip_ranges,
1246 rc = cfs_ip_addr_match(bswap_32(ip),
1247 &ip_range->ipr_expr);
1251 rc = lustre_lnet_add_intf_descr
1252 (intf_list, ifaddr->ifa_name,
1253 strlen(ifaddr->ifa_name));
1255 if (rc != LUSTRE_CFG_RC_NO_ERR)
1261 if (!list_empty(intf_list))
1262 return LUSTRE_CFG_RC_MATCH;
1264 return LUSTRE_CFG_RC_NO_MATCH;
1268 * If an interface is explicitly specified the ip-range might or
1269 * might not be specified. if specified the interface needs to match the
1270 * ip-range. If no ip-range then the interfaces are
1271 * automatically matched if they are all up.
1272 * If > 1 interfaces all the interfaces must match for the NI to
1275 list_for_each_entry_safe(intf_descr, tmp, intf_list, intf_on_network) {
1276 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1277 if (ifaddr->ifa_addr == NULL)
1280 family = ifaddr->ifa_addr->sa_family;
1281 if (family == AF_INET &&
1282 strcmp(intf_descr->intf_name,
1283 ifaddr->ifa_name) == 0)
1287 if (ifaddr == NULL) {
1288 list_del(&intf_descr->intf_on_network);
1289 free_intf_descr(intf_descr);
1293 if ((ifaddr->ifa_flags & IFF_UP) == 0) {
1294 list_del(&intf_descr->intf_on_network);
1295 free_intf_descr(intf_descr);
1299 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr.s_addr;
1302 list_for_each_entry(ip_range, ip_ranges, ipr_entry) {
1303 rc = cfs_ip_addr_match(bswap_32(ip), &ip_range->ipr_expr);
1309 /* no match for this interface */
1310 list_del(&intf_descr->intf_on_network);
1311 free_intf_descr(intf_descr);
1315 return LUSTRE_CFG_RC_MATCH;
1318 int lustre_lnet_resolve_ip2nets_rule(struct lustre_lnet_ip2nets *ip2nets,
1319 lnet_nid_t **nids, __u32 *nnids)
1321 struct ifaddrs *ifa;
1322 int rc = LUSTRE_CFG_RC_NO_ERR;
1324 rc = getifaddrs(&ifa);
1328 rc = lustre_lnet_match_ip_to_intf(ifa,
1329 &ip2nets->ip2nets_net.nw_intflist,
1330 &ip2nets->ip2nets_ip_ranges);
1331 if (rc != LUSTRE_CFG_RC_MATCH) {
1336 rc = lustre_lnet_intf2nids(&ip2nets->ip2nets_net, nids, nnids);
1337 if (rc != LUSTRE_CFG_RC_NO_ERR) {
1348 lustre_lnet_ioctl_config_ni(struct list_head *intf_list,
1349 struct lnet_ioctl_config_lnd_tunables *tunables,
1350 struct cfs_expr_list *global_cpts,
1351 lnet_nid_t *nids, char *err_str)
1354 struct lnet_ioctl_config_ni *conf;
1355 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1356 int rc = LUSTRE_CFG_RC_NO_ERR, i = 0;
1359 struct lnet_dlc_intf_descr *intf_descr;
1361 struct cfs_expr_list *cpt_expr;
1363 list_for_each_entry(intf_descr, intf_list,
1365 if (tunables != NULL)
1366 len = sizeof(struct lnet_ioctl_config_ni) +
1367 sizeof(struct lnet_ioctl_config_lnd_tunables);
1369 len = sizeof(struct lnet_ioctl_config_ni);
1371 data = calloc(1, len);
1373 return LUSTRE_CFG_RC_OUT_OF_MEM;
1374 conf = (struct lnet_ioctl_config_ni*) data;
1375 if (tunables != NULL)
1376 tun = (struct lnet_ioctl_config_lnd_tunables*)
1379 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1380 conf->lic_cfg_hdr.ioc_len = len;
1381 conf->lic_nid = nids[i];
1382 strncpy(conf->lic_ni_intf[0], intf_descr->intf_name,
1385 if (intf_descr->cpt_expr != NULL)
1386 cpt_expr = intf_descr->cpt_expr;
1387 else if (global_cpts != NULL)
1388 cpt_expr = global_cpts;
1392 if (cpt_expr != NULL) {
1393 count = cfs_expr_list_values(cpt_expr,
1394 LNET_MAX_SHOW_NUM_CPT,
1397 memcpy(conf->lic_cpts, cpt_array,
1398 sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1407 conf->lic_ncpts = count;
1409 if (tunables != NULL)
1410 memcpy(tun, tunables, sizeof(*tunables));
1412 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1417 "\"cannot add network: %s\"", strerror(errno));
1425 return LUSTRE_CFG_RC_NO_ERR;
1429 lustre_lnet_config_ip2nets(struct lustre_lnet_ip2nets *ip2nets,
1430 struct lnet_ioctl_config_lnd_tunables *tunables,
1431 struct cfs_expr_list *global_cpts,
1432 int seq_no, struct cYAML **err_rc)
1434 lnet_nid_t *nids = NULL;
1437 char err_str[LNET_MAX_STR_LEN];
1439 snprintf(err_str, sizeof(err_str), "\"success\"");
1444 "\"incomplete ip2nets information\"");
1445 rc = LUSTRE_CFG_RC_BAD_PARAM;
1450 * call below function to resolve the rules into a list of nids.
1451 * The memory is allocated in that function then freed here when
1452 * it's no longer needed.
1454 rc = lustre_lnet_resolve_ip2nets_rule(ip2nets, &nids, &nnids);
1455 if (rc != LUSTRE_CFG_RC_NO_ERR && rc != LUSTRE_CFG_RC_MATCH) {
1458 "\"cannot resolve ip2nets rule\"");
1462 if (list_empty(&ip2nets->ip2nets_net.nw_intflist)) {
1463 snprintf(err_str, sizeof(err_str),
1464 "\"no interfaces match ip2nets rules\"");
1468 rc = lustre_lnet_ioctl_config_ni(&ip2nets->ip2nets_net.nw_intflist,
1469 tunables, global_cpts, nids,
1476 cYAML_build_error(rc, seq_no, ADD_CMD, "ip2nets", err_str, err_rc);
1480 int lustre_lnet_config_ni(struct lnet_dlc_network_descr *nw_descr,
1481 struct cfs_expr_list *global_cpts,
1483 struct lnet_ioctl_config_lnd_tunables *tunables,
1484 int seq_no, struct cYAML **err_rc)
1487 struct lnet_ioctl_config_ni *conf;
1488 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1489 char buf[LNET_MAX_STR_LEN];
1490 int rc = LUSTRE_CFG_RC_NO_ERR;
1491 char err_str[LNET_MAX_STR_LEN];
1492 lnet_nid_t *nids = NULL;
1496 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1499 snprintf(err_str, sizeof(err_str), "\"success\"");
1501 if (ip2net == NULL && nw_descr == NULL) {
1504 "\"mandatory parameters not specified.\"");
1505 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1509 if (ip2net != NULL && strlen(ip2net) >= sizeof(buf)) {
1512 "\"ip2net string too long %d\"",
1513 (int)strlen(ip2net));
1514 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1518 if (ip2net != NULL) {
1519 if (tunables != NULL)
1520 len = sizeof(struct lnet_ioctl_config_ni) +
1521 sizeof(struct lnet_ioctl_config_lnd_tunables);
1523 len = sizeof(struct lnet_ioctl_config_ni);
1524 data = calloc(1, len);
1526 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1529 conf = (struct lnet_ioctl_config_ni*) data;
1530 if (tunables != NULL)
1531 tun = (struct lnet_ioctl_config_lnd_tunables*)
1532 (data + sizeof(*conf));
1534 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1535 conf->lic_cfg_hdr.ioc_len = len;
1536 strncpy(conf->lic_legacy_ip2nets, ip2net,
1539 if (global_cpts != NULL) {
1540 count = cfs_expr_list_values(global_cpts,
1541 LNET_MAX_SHOW_NUM_CPT,
1544 memcpy(conf->lic_cpts, cpt_array,
1545 sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1554 conf->lic_ncpts = count;
1556 if (tunables != NULL)
1557 memcpy(tun, tunables, sizeof(*tunables));
1559 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1564 "\"cannot add network: %s\"", strerror(errno));
1571 if (LNET_NETTYP(nw_descr->nw_id) == LOLND) {
1572 rc = LUSTRE_CFG_RC_NO_ERR;
1576 if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1579 "\"cannot parse net '%s'\"",
1580 libcfs_net2str(nw_descr->nw_id));
1581 rc = LUSTRE_CFG_RC_BAD_PARAM;
1585 if (list_empty(&nw_descr->nw_intflist)) {
1588 "\"no interface name provided\"");
1589 rc = LUSTRE_CFG_RC_BAD_PARAM;
1593 rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids);
1595 snprintf(err_str, sizeof(err_str),
1596 "\"bad parameter\"");
1597 rc = LUSTRE_CFG_RC_BAD_PARAM;
1601 rc = lustre_lnet_ioctl_config_ni(&nw_descr->nw_intflist,
1602 tunables, global_cpts, nids,
1606 if (nw_descr != NULL) {
1607 list_for_each_entry_safe(intf_descr, tmp,
1608 &nw_descr->nw_intflist,
1610 list_del(&intf_descr->intf_on_network);
1611 free_intf_descr(intf_descr);
1615 cYAML_build_error(rc, seq_no, ADD_CMD, "net", err_str, err_rc);
1626 int lustre_lnet_del_ni(struct lnet_dlc_network_descr *nw_descr,
1627 int seq_no, struct cYAML **err_rc)
1629 struct lnet_ioctl_config_ni data;
1630 int rc = LUSTRE_CFG_RC_NO_ERR, i;
1631 char err_str[LNET_MAX_STR_LEN];
1632 lnet_nid_t *nids = NULL;
1634 struct lnet_dlc_intf_descr *intf_descr, *tmp;
1636 snprintf(err_str, sizeof(err_str), "\"success\"");
1638 if (nw_descr == NULL) {
1641 "\"missing mandatory parameter\"");
1642 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1646 if (LNET_NETTYP(nw_descr->nw_id) == LOLND)
1647 return LUSTRE_CFG_RC_NO_ERR;
1649 if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1652 "\"cannot parse net '%s'\"",
1653 libcfs_net2str(nw_descr->nw_id));
1654 rc = LUSTRE_CFG_RC_BAD_PARAM;
1658 rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids);
1660 snprintf(err_str, sizeof(err_str),
1661 "\"bad parameter\"");
1662 rc = LUSTRE_CFG_RC_BAD_PARAM;
1667 * no interfaces just the nw_id is specified
1670 nids = calloc(1, sizeof(*nids));
1672 snprintf(err_str, sizeof(err_str),
1673 "\"out of memory\"");
1674 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1677 nids[0] = LNET_MKNID(nw_descr->nw_id, 0);
1681 for (i = 0; i < nnids; i++) {
1682 LIBCFS_IOC_INIT_V2(data, lic_cfg_hdr);
1683 data.lic_nid = nids[i];
1685 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_LOCAL_NI, &data);
1690 "\"cannot del network: %s\"", strerror(errno));
1694 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
1696 list_del(&intf_descr->intf_on_network);
1697 free_intf_descr(intf_descr);
1701 cYAML_build_error(rc, seq_no, DEL_CMD, "net", err_str, err_rc);
1709 int lustre_lnet_show_net(char *nw, int detail, int seq_no,
1710 struct cYAML **show_rc, struct cYAML **err_rc)
1713 struct lnet_ioctl_config_ni *ni_data;
1714 struct lnet_ioctl_config_lnd_tunables *lnd;
1715 struct lnet_ioctl_element_stats *stats;
1716 __u32 net = LNET_NIDNET(LNET_NID_ANY);
1717 __u32 prev_net = LNET_NIDNET(LNET_NID_ANY);
1718 int rc = LUSTRE_CFG_RC_OUT_OF_MEM, i, j;
1720 struct cYAML *root = NULL, *tunables = NULL,
1721 *net_node = NULL, *interfaces = NULL,
1722 *item = NULL, *first_seq = NULL,
1723 *tmp = NULL, *statistics = NULL;
1724 int str_buf_len = LNET_MAX_SHOW_NUM_CPT * 2;
1725 char str_buf[str_buf_len];
1727 char err_str[LNET_MAX_STR_LEN];
1728 bool exist = false, new_net = true;
1730 size_t buf_size = sizeof(*ni_data) + sizeof(*lnd) + sizeof(*stats);
1732 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1734 buf = calloc(1, buf_size);
1738 ni_data = (struct lnet_ioctl_config_ni *)buf;
1741 net = libcfs_str2net(nw);
1742 if (net == LNET_NIDNET(LNET_NID_ANY)) {
1745 "\"cannot parse net '%s'\"", nw);
1746 rc = LUSTRE_CFG_RC_BAD_PARAM;
1751 root = cYAML_create_object(NULL, NULL);
1755 net_node = cYAML_create_seq(root, "net");
1756 if (net_node == NULL)
1763 memset(buf, 0, buf_size);
1765 LIBCFS_IOC_INIT_V2(*ni_data, lic_cfg_hdr);
1767 * set the ioc_len to the proper value since INIT assumes
1770 ni_data->lic_cfg_hdr.ioc_len = buf_size;
1771 ni_data->lic_idx = i;
1773 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LOCAL_NI, ni_data);
1779 rc_net = LNET_NIDNET(ni_data->lic_nid);
1781 /* filter on provided data */
1782 if (net != LNET_NIDNET(LNET_NID_ANY) &&
1786 /* default rc to -1 in case we hit the goto */
1790 stats = (struct lnet_ioctl_element_stats *)ni_data->lic_bulk;
1791 lnd = (struct lnet_ioctl_config_lnd_tunables *)
1792 (ni_data->lic_bulk + sizeof(*stats));
1794 if (rc_net != prev_net) {
1801 if (!cYAML_create_string(net_node, "net type",
1802 libcfs_net2str(rc_net)))
1805 tmp = cYAML_create_seq(net_node, "local NI(s)");
1811 /* create the tree to be printed. */
1812 item = cYAML_create_seq_item(tmp);
1816 if (first_seq == NULL)
1819 if (cYAML_create_string(item, "nid",
1820 libcfs_nid2str(ni_data->lic_nid)) == NULL)
1823 if (cYAML_create_string(item,
1825 (ni_data->lic_status ==
1826 LNET_NI_STATUS_UP) ?
1827 "up" : "down") == NULL)
1830 /* don't add interfaces unless there is at least one
1832 if (strlen(ni_data->lic_ni_intf[0]) > 0) {
1833 interfaces = cYAML_create_object(item, "interfaces");
1834 if (interfaces == NULL)
1837 for (j = 0; j < LNET_NUM_INTERFACES; j++) {
1838 if (strlen(ni_data->lic_ni_intf[j]) > 0) {
1840 sizeof(str_buf), "%d", j);
1841 if (cYAML_create_string(interfaces,
1843 ni_data->lic_ni_intf[j]) ==
1853 statistics = cYAML_create_object(item, "statistics");
1854 if (statistics == NULL)
1857 if (cYAML_create_number(statistics, "send_count",
1858 stats->iel_send_count)
1862 if (cYAML_create_number(statistics, "recv_count",
1863 stats->iel_recv_count)
1867 if (cYAML_create_number(statistics, "drop_count",
1868 stats->iel_drop_count)
1872 tunables = cYAML_create_object(item, "tunables");
1876 rc = lustre_net_show_tunables(tunables, &lnd->lt_cmn);
1877 if (rc != LUSTRE_CFG_RC_NO_ERR)
1880 tunables = cYAML_create_object(item, "lnd tunables");
1881 if (tunables == NULL)
1884 rc = lustre_ni_show_tunables(tunables, LNET_NETTYP(rc_net),
1886 if (rc != LUSTRE_CFG_RC_NO_ERR)
1889 if (cYAML_create_number(item, "tcp bonding",
1890 ni_data->lic_tcp_bonding)
1894 if (cYAML_create_number(item, "dev cpt",
1895 ni_data->lic_dev_cpt) == NULL)
1898 /* out put the CPTs in the format: "[x,x,x,...]" */
1899 limit = str_buf + str_buf_len - 3;
1900 pos += snprintf(pos, limit - pos, "\"[");
1901 for (j = 0 ; ni_data->lic_ncpts >= 1 &&
1902 j < ni_data->lic_ncpts &&
1904 pos += snprintf(pos, limit - pos,
1905 "%d", ni_data->lic_cpts[j]);
1906 if ((j + 1) < ni_data->lic_ncpts)
1907 pos += snprintf(pos, limit - pos, ",");
1909 pos += snprintf(pos, 3, "]\"");
1911 if (ni_data->lic_ncpts >= 1 &&
1912 cYAML_create_string(item, "CPT",
1918 /* Print out the net information only if show_rc is not provided */
1919 if (show_rc == NULL)
1920 cYAML_print_tree(root);
1922 if (l_errno != ENOENT) {
1925 "\"cannot get networks: %s\"",
1930 rc = LUSTRE_CFG_RC_NO_ERR;
1932 snprintf(err_str, sizeof(err_str), "\"success\"");
1934 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
1935 cYAML_free_tree(root);
1936 } else if (show_rc != NULL && *show_rc != NULL) {
1937 struct cYAML *show_node;
1938 /* find the net node, if one doesn't exist
1939 * then insert one. Otherwise add to the one there
1941 show_node = cYAML_get_object_item(*show_rc, "net");
1942 if (show_node != NULL && cYAML_is_sequence(show_node)) {
1943 cYAML_insert_child(show_node, first_seq);
1946 } else if (show_node == NULL) {
1947 cYAML_insert_sibling((*show_rc)->cy_child,
1951 cYAML_free_tree(root);
1957 cYAML_build_error(rc, seq_no, SHOW_CMD, "net", err_str, err_rc);
1962 int lustre_lnet_enable_routing(int enable, int seq_no, struct cYAML **err_rc)
1964 struct lnet_ioctl_config_data data;
1965 int rc = LUSTRE_CFG_RC_NO_ERR;
1966 char err_str[LNET_MAX_STR_LEN];
1968 snprintf(err_str, sizeof(err_str), "\"success\"");
1970 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1971 data.cfg_config_u.cfg_buffers.buf_enable = (enable) ? 1 : 0;
1973 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIG_RTR, &data);
1978 "\"cannot %s routing %s\"",
1979 (enable) ? "enable" : "disable", strerror(errno));
1984 cYAML_build_error(rc, seq_no,
1985 (enable) ? ADD_CMD : DEL_CMD,
1986 "routing", err_str, err_rc);
1991 int lustre_lnet_config_numa_range(int range, int seq_no, struct cYAML **err_rc)
1993 struct lnet_ioctl_numa_range data;
1994 int rc = LUSTRE_CFG_RC_NO_ERR;
1995 char err_str[LNET_MAX_STR_LEN];
1997 snprintf(err_str, sizeof(err_str), "\"success\"");
2002 "\"range must be >= 0\"");
2003 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
2007 LIBCFS_IOC_INIT_V2(data, nr_hdr);
2008 data.nr_range = range;
2010 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_SET_NUMA_RANGE, &data);
2015 "\"cannot configure buffers: %s\"", strerror(errno));
2020 cYAML_build_error(rc, seq_no, ADD_CMD, "numa_range", err_str, err_rc);
2025 int lustre_lnet_config_buffers(int tiny, int small, int large, int seq_no,
2026 struct cYAML **err_rc)
2028 struct lnet_ioctl_config_data data;
2029 int rc = LUSTRE_CFG_RC_NO_ERR;
2030 char err_str[LNET_MAX_STR_LEN];
2032 snprintf(err_str, sizeof(err_str), "\"success\"");
2034 /* -1 indicates to ignore changes to this field */
2035 if (tiny < -1 || small < -1 || large < -1) {
2038 "\"tiny, small and large must be >= 0\"");
2039 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
2043 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2044 data.cfg_config_u.cfg_buffers.buf_tiny = tiny;
2045 data.cfg_config_u.cfg_buffers.buf_small = small;
2046 data.cfg_config_u.cfg_buffers.buf_large = large;
2048 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_BUF, &data);
2053 "\"cannot configure buffers: %s\"", strerror(errno));
2058 cYAML_build_error(rc, seq_no, ADD_CMD, "buf", err_str, err_rc);
2063 int lustre_lnet_show_routing(int seq_no, struct cYAML **show_rc,
2064 struct cYAML **err_rc)
2066 struct lnet_ioctl_config_data *data;
2067 struct lnet_ioctl_pool_cfg *pool_cfg = NULL;
2068 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2071 char *pools[LNET_NRBPOOLS] = {"tiny", "small", "large"};
2072 int buf_count[LNET_NRBPOOLS] = {0};
2073 struct cYAML *root = NULL, *pools_node = NULL,
2074 *type_node = NULL, *item = NULL, *cpt = NULL,
2075 *first_seq = NULL, *buffers = NULL;
2077 char err_str[LNET_MAX_STR_LEN];
2078 char node_name[LNET_MAX_STR_LEN];
2081 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2083 buf = calloc(1, sizeof(*data) + sizeof(*pool_cfg));
2087 data = (struct lnet_ioctl_config_data *)buf;
2089 root = cYAML_create_object(NULL, NULL);
2093 pools_node = cYAML_create_seq(root, "routing");
2094 if (pools_node == NULL)
2098 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
2099 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
2100 sizeof(struct lnet_ioctl_pool_cfg);
2101 data->cfg_count = i;
2103 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_BUF, data);
2111 pool_cfg = (struct lnet_ioctl_pool_cfg *)data->cfg_bulk;
2113 snprintf(node_name, sizeof(node_name), "cpt[%d]", i);
2114 item = cYAML_create_seq_item(pools_node);
2118 if (first_seq == NULL)
2121 cpt = cYAML_create_object(item, node_name);
2125 /* create the tree and print */
2126 for (j = 0; j < LNET_NRBPOOLS; j++) {
2127 type_node = cYAML_create_object(cpt, pools[j]);
2128 if (type_node == NULL)
2130 if (cYAML_create_number(type_node, "npages",
2131 pool_cfg->pl_pools[j].pl_npages)
2134 if (cYAML_create_number(type_node, "nbuffers",
2135 pool_cfg->pl_pools[j].
2136 pl_nbuffers) == NULL)
2138 if (cYAML_create_number(type_node, "credits",
2139 pool_cfg->pl_pools[j].
2140 pl_credits) == NULL)
2142 if (cYAML_create_number(type_node, "mincredits",
2143 pool_cfg->pl_pools[j].
2144 pl_mincredits) == NULL)
2146 /* keep track of the total count for each of the
2147 * tiny, small and large buffers */
2148 buf_count[j] += pool_cfg->pl_pools[j].pl_nbuffers;
2152 if (pool_cfg != NULL) {
2153 item = cYAML_create_seq_item(pools_node);
2157 if (cYAML_create_number(item, "enable", pool_cfg->pl_routing) ==
2162 /* create a buffers entry in the show. This is necessary so that
2163 * if the YAML output is used to configure a node, the buffer
2164 * configuration takes hold */
2165 buffers = cYAML_create_object(root, "buffers");
2166 if (buffers == NULL)
2169 for (i = 0; i < LNET_NRBPOOLS; i++) {
2170 if (cYAML_create_number(buffers, pools[i], buf_count[i]) == NULL)
2174 if (show_rc == NULL)
2175 cYAML_print_tree(root);
2177 if (l_errno != ENOENT) {
2180 "\"cannot get routing information: %s\"",
2185 rc = LUSTRE_CFG_RC_NO_ERR;
2187 snprintf(err_str, sizeof(err_str), "\"success\"");
2188 rc = LUSTRE_CFG_RC_NO_ERR;
2192 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2193 cYAML_free_tree(root);
2194 } else if (show_rc != NULL && *show_rc != NULL) {
2195 struct cYAML *routing_node;
2196 /* there should exist only one routing block and one
2197 * buffers block. If there already exists a previous one
2198 * then don't add another */
2199 routing_node = cYAML_get_object_item(*show_rc, "routing");
2200 if (routing_node == NULL) {
2201 cYAML_insert_sibling((*show_rc)->cy_child,
2205 cYAML_free_tree(root);
2211 cYAML_build_error(rc, seq_no, SHOW_CMD, "routing", err_str, err_rc);
2216 int lustre_lnet_show_peer(char *knid, int detail, int seq_no,
2217 struct cYAML **show_rc, struct cYAML **err_rc)
2220 * TODO: This function is changing in a future patch to accommodate
2221 * PEER_LIST and proper filtering on any nid of the peer
2223 struct lnet_ioctl_peer_cfg peer_info;
2224 struct lnet_peer_ni_credit_info *lpni_cri;
2225 struct lnet_ioctl_element_stats *lpni_stats;
2226 int rc = LUSTRE_CFG_RC_OUT_OF_MEM, ncpt = 0, i = 0, j = 0;
2228 struct cYAML *root = NULL, *peer = NULL, *peer_ni = NULL,
2229 *first_seq = NULL, *peer_root = NULL, *tmp = NULL;
2230 char err_str[LNET_MAX_STR_LEN];
2231 lnet_nid_t prev_primary_nid = LNET_NID_ANY, primary_nid = LNET_NID_ANY;
2232 int data_size = sizeof(*lpni_cri) + sizeof(*lpni_stats);
2233 char *data = malloc(data_size);
2234 bool new_peer = true;
2236 snprintf(err_str, sizeof(err_str),
2237 "\"out of memory\"");
2242 /* create struct cYAML root object */
2243 root = cYAML_create_object(NULL, NULL);
2247 peer_root = cYAML_create_seq(root, "peer");
2248 if (peer_root == NULL)
2252 primary_nid = libcfs_str2nid(knid);
2256 memset(data, 0, data_size);
2257 memset(&peer_info, 0, sizeof(peer_info));
2258 LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2259 peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2260 peer_info.prcfg_count = i;
2261 peer_info.prcfg_bulk = (void *)data;
2262 peer_info.prcfg_size = data_size;
2264 rc = l_ioctl(LNET_DEV_ID,
2265 IOC_LIBCFS_GET_PEER_NI, &peer_info);
2271 if (primary_nid != LNET_NID_ANY &&
2272 primary_nid != peer_info.prcfg_prim_nid)
2275 lpni_cri = peer_info.prcfg_bulk;
2276 lpni_stats = peer_info.prcfg_bulk + sizeof(*lpni_cri);
2278 peer = cYAML_create_seq_item(peer_root);
2282 if (peer_info.prcfg_prim_nid != prev_primary_nid) {
2283 prev_primary_nid = peer_info.prcfg_prim_nid;
2288 lnet_nid_t pnid = peer_info.prcfg_prim_nid;
2289 if (cYAML_create_string(peer, "primary nid",
2290 libcfs_nid2str(pnid))
2293 if (cYAML_create_string(peer, "Multi-Rail",
2294 peer_info.prcfg_mr ?
2298 tmp = cYAML_create_seq(peer, "peer ni");
2304 if (first_seq == NULL)
2307 peer_ni = cYAML_create_seq_item(tmp);
2308 if (peer_ni == NULL)
2311 if (cYAML_create_string(peer_ni, "nid",
2313 (peer_info.prcfg_cfg_nid))
2317 if (cYAML_create_string(peer_ni, "state",
2318 lpni_cri->cr_aliveness)
2325 if (cYAML_create_number(peer_ni, "max_ni_tx_credits",
2326 lpni_cri->cr_ni_peer_tx_credits)
2330 if (cYAML_create_number(peer_ni, "available_tx_credits",
2331 lpni_cri->cr_peer_tx_credits)
2335 if (cYAML_create_number(peer_ni, "min_tx_credits",
2336 lpni_cri->cr_peer_min_tx_credits)
2340 if (cYAML_create_number(peer_ni, "tx_q_num_of_buf",
2341 lpni_cri->cr_peer_tx_qnob)
2345 if (cYAML_create_number(peer_ni, "available_rtr_credits",
2346 lpni_cri->cr_peer_rtr_credits)
2350 if (cYAML_create_number(peer_ni, "min_rtr_credits",
2351 lpni_cri->cr_peer_min_rtr_credits)
2355 if (cYAML_create_number(peer_ni, "send_count",
2356 lpni_stats->iel_send_count)
2360 if (cYAML_create_number(peer_ni, "recv_count",
2361 lpni_stats->iel_recv_count)
2365 if (cYAML_create_number(peer_ni, "drop_count",
2366 lpni_stats->iel_drop_count)
2370 if (cYAML_create_number(peer_ni, "refcount",
2371 lpni_cri->cr_refcount) == NULL)
2375 if (l_errno != ENOENT) {
2378 "\"cannot get peer information: %s\"",
2387 /* print output iff show_rc is not provided */
2388 if (show_rc == NULL)
2389 cYAML_print_tree(root);
2391 snprintf(err_str, sizeof(err_str), "\"success\"");
2392 rc = LUSTRE_CFG_RC_NO_ERR;
2395 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2396 cYAML_free_tree(root);
2397 } else if (show_rc != NULL && *show_rc != NULL) {
2398 struct cYAML *show_node;
2399 /* find the peer node, if one doesn't exist then
2400 * insert one. Otherwise add to the one there
2402 show_node = cYAML_get_object_item(*show_rc,
2404 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2405 cYAML_insert_child(show_node, first_seq);
2408 } else if (show_node == NULL) {
2409 cYAML_insert_sibling((*show_rc)->cy_child,
2413 cYAML_free_tree(root);
2419 cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
2425 int lustre_lnet_show_numa_range(int seq_no, struct cYAML **show_rc,
2426 struct cYAML **err_rc)
2428 struct lnet_ioctl_numa_range data;
2431 char err_str[LNET_MAX_STR_LEN];
2432 struct cYAML *root = NULL, *range = NULL;
2434 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2436 LIBCFS_IOC_INIT_V2(data, nr_hdr);
2438 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NUMA_RANGE, &data);
2443 "\"cannot get numa range: %s\"",
2449 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2451 root = cYAML_create_object(NULL, NULL);
2455 range = cYAML_create_object(root, "numa");
2459 if (cYAML_create_number(range, "range",
2460 data.nr_range) == NULL)
2463 if (show_rc == NULL)
2464 cYAML_print_tree(root);
2466 snprintf(err_str, sizeof(err_str), "\"success\"");
2467 rc = LUSTRE_CFG_RC_NO_ERR;
2469 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2470 cYAML_free_tree(root);
2471 } else if (show_rc != NULL && *show_rc != NULL) {
2472 cYAML_insert_sibling((*show_rc)->cy_child,
2479 cYAML_build_error(rc, seq_no, SHOW_CMD, "numa", err_str, err_rc);
2484 int lustre_lnet_show_stats(int seq_no, struct cYAML **show_rc,
2485 struct cYAML **err_rc)
2487 struct lnet_ioctl_lnet_stats data;
2490 char err_str[LNET_MAX_STR_LEN];
2491 struct cYAML *root = NULL, *stats = NULL;
2493 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2495 LIBCFS_IOC_INIT_V2(data, st_hdr);
2497 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LNET_STATS, &data);
2502 "\"cannot get lnet statistics: %s\"",
2508 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2510 root = cYAML_create_object(NULL, NULL);
2514 stats = cYAML_create_object(root, "statistics");
2518 if (cYAML_create_number(stats, "msgs_alloc",
2519 data.st_cntrs.msgs_alloc) == NULL)
2522 if (cYAML_create_number(stats, "msgs_max",
2523 data.st_cntrs.msgs_max) == NULL)
2526 if (cYAML_create_number(stats, "errors",
2527 data.st_cntrs.errors) == NULL)
2530 if (cYAML_create_number(stats, "send_count",
2531 data.st_cntrs.send_count) == NULL)
2534 if (cYAML_create_number(stats, "recv_count",
2535 data.st_cntrs.recv_count) == NULL)
2538 if (cYAML_create_number(stats, "route_count",
2539 data.st_cntrs.route_count) == NULL)
2542 if (cYAML_create_number(stats, "drop_count",
2543 data.st_cntrs.drop_count) == NULL)
2546 if (cYAML_create_number(stats, "send_length",
2547 data.st_cntrs.send_length) == NULL)
2550 if (cYAML_create_number(stats, "recv_length",
2551 data.st_cntrs.recv_length) == NULL)
2554 if (cYAML_create_number(stats, "route_length",
2555 data.st_cntrs.route_length) == NULL)
2558 if (cYAML_create_number(stats, "drop_length",
2559 data.st_cntrs.drop_length) == NULL)
2562 if (show_rc == NULL)
2563 cYAML_print_tree(root);
2565 snprintf(err_str, sizeof(err_str), "\"success\"");
2566 rc = LUSTRE_CFG_RC_NO_ERR;
2568 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2569 cYAML_free_tree(root);
2570 } else if (show_rc != NULL && *show_rc != NULL) {
2571 cYAML_insert_sibling((*show_rc)->cy_child,
2578 cYAML_build_error(rc, seq_no, SHOW_CMD, "statistics", err_str, err_rc);
2583 typedef int (*cmd_handler_t)(struct cYAML *tree,
2584 struct cYAML **show_rc,
2585 struct cYAML **err_rc);
2587 static int handle_yaml_config_route(struct cYAML *tree, struct cYAML **show_rc,
2588 struct cYAML **err_rc)
2590 struct cYAML *net, *gw, *hop, *prio, *seq_no;
2592 net = cYAML_get_object_item(tree, "net");
2593 gw = cYAML_get_object_item(tree, "gateway");
2594 hop = cYAML_get_object_item(tree, "hop");
2595 prio = cYAML_get_object_item(tree, "priority");
2596 seq_no = cYAML_get_object_item(tree, "seq_no");
2598 return lustre_lnet_config_route((net) ? net->cy_valuestring : NULL,
2599 (gw) ? gw->cy_valuestring : NULL,
2600 (hop) ? hop->cy_valueint : -1,
2601 (prio) ? prio->cy_valueint : -1,
2602 (seq_no) ? seq_no->cy_valueint : -1,
2606 static void yaml_free_string_array(char **array, int num)
2609 char **sub_array = array;
2611 for (i = 0; i < num; i++) {
2612 if (*sub_array != NULL)
2622 * 0: <intf_name>['['<expr>']']
2623 * 1: <intf_name>['['<expr>']']
2625 static int yaml_copy_intf_info(struct cYAML *intf_tree,
2626 struct lnet_dlc_network_descr *nw_descr)
2628 struct cYAML *child = NULL;
2629 int intf_num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
2630 struct lnet_dlc_intf_descr *intf_descr, *tmp;
2632 if (intf_tree == NULL || nw_descr == NULL)
2633 return LUSTRE_CFG_RC_BAD_PARAM;
2635 /* now grab all the interfaces and their cpts */
2636 child = intf_tree->cy_child;
2637 while (child != NULL) {
2638 if (child->cy_valuestring == NULL) {
2639 child = child->cy_next;
2643 if (strlen(child->cy_valuestring) >= LNET_MAX_STR_LEN)
2646 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist,
2647 child->cy_valuestring,
2648 strlen(child->cy_valuestring));
2649 if (rc != LUSTRE_CFG_RC_NO_ERR)
2653 child = child->cy_next;
2657 return LUSTRE_CFG_RC_MISSING_PARAM;
2662 list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
2664 list_del(&intf_descr->intf_on_network);
2665 free_intf_descr(intf_descr);
2672 yaml_extract_cmn_tunables(struct cYAML *tree,
2673 struct lnet_ioctl_config_lnd_cmn_tunables *tunables,
2674 struct cfs_expr_list **global_cpts)
2676 struct cYAML *tun, *item, *smp;
2679 tun = cYAML_get_object_item(tree, "tunables");
2681 item = cYAML_get_object_item(tun, "peer_timeout");
2683 tunables->lct_peer_timeout = item->cy_valueint;
2684 item = cYAML_get_object_item(tun, "peer_credits");
2686 tunables->lct_peer_tx_credits = item->cy_valueint;
2687 item = cYAML_get_object_item(tun, "peer_buffer_credits");
2689 tunables->lct_peer_rtr_credits = item->cy_valueint;
2690 item = cYAML_get_object_item(tun, "credits");
2692 tunables->lct_max_tx_credits = item->cy_valueint;
2693 smp = cYAML_get_object_item(tun, "CPT");
2695 rc = cfs_expr_list_parse(smp->cy_valuestring,
2696 strlen(smp->cy_valuestring),
2697 0, UINT_MAX, global_cpts);
2699 *global_cpts = NULL;
2709 yaml_extract_tunables(struct cYAML *tree,
2710 struct lnet_ioctl_config_lnd_tunables *tunables,
2711 struct cfs_expr_list **global_cpts,
2716 rc = yaml_extract_cmn_tunables(tree, &tunables->lt_cmn,
2722 lustre_yaml_extract_lnd_tunables(tree, net_type,
2730 * - net type: <net>[<NUM>]
2732 * - nid: <ip>@<net>[<NUM>]
2735 * 0: <intf_name>['['<expr>']']
2736 * 1: <intf_name>['['<expr>']']
2738 * peer_timeout: <NUM>
2739 * peer_credits: <NUM>
2740 * peer_buffer_credits: <NUM>
2743 * peercredits_hiw: <NUM>
2744 * map_on_demand: <NUM>
2745 * concurrent_sends: <NUM>
2746 * fmr_pool_size: <NUM>
2747 * fmr_flush_trigger: <NUM>
2750 * At least one interface is required. If no interfaces are provided the
2751 * network interface can not be configured.
2753 static int handle_yaml_config_ni(struct cYAML *tree, struct cYAML **show_rc,
2754 struct cYAML **err_rc)
2756 struct cYAML *net, *intf, *seq_no, *ip2net = NULL, *local_nis = NULL,
2758 int num_entries = 0, rc;
2759 struct lnet_dlc_network_descr nw_descr;
2760 struct cfs_expr_list *global_cpts = NULL;
2761 struct lnet_ioctl_config_lnd_tunables tunables;
2764 memset(&tunables, 0, sizeof(tunables));
2766 INIT_LIST_HEAD(&nw_descr.network_on_rule);
2767 INIT_LIST_HEAD(&nw_descr.nw_intflist);
2769 ip2net = cYAML_get_object_item(tree, "ip2net");
2770 net = cYAML_get_object_item(tree, "net type");
2772 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
2774 nw_descr.nw_id = LOLND;
2777 * if neither net nor ip2nets are present, then we can not
2778 * configure the network.
2780 if (!net && !ip2net)
2781 return LUSTRE_CFG_RC_MISSING_PARAM;
2783 local_nis = cYAML_get_object_item(tree, "local NI(s)");
2784 if (local_nis == NULL)
2785 return LUSTRE_CFG_RC_MISSING_PARAM;
2787 if (!cYAML_is_sequence(local_nis))
2788 return LUSTRE_CFG_RC_BAD_PARAM;
2790 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
2791 intf = cYAML_get_object_item(item, "interfaces");
2794 num_entries = yaml_copy_intf_info(intf, &nw_descr);
2795 if (num_entries <= 0) {
2796 cYAML_build_error(num_entries, -1, "ni", "add",
2797 "bad interface list",
2799 return LUSTRE_CFG_RC_BAD_PARAM;
2803 found = yaml_extract_tunables(tree, &tunables, &global_cpts,
2804 LNET_NETTYP(nw_descr.nw_id));
2805 seq_no = cYAML_get_object_item(tree, "seq_no");
2807 rc = lustre_lnet_config_ni(&nw_descr,
2809 (ip2net) ? ip2net->cy_valuestring : NULL,
2810 (found) ? &tunables: NULL,
2811 (seq_no) ? seq_no->cy_valueint : -1,
2814 if (global_cpts != NULL)
2815 cfs_expr_list_free(global_cpts);
2822 * - net-spec: <tcp|o2ib|gni>[NUM]
2824 * 0: <intf name>['['<expr>']']
2825 * 1: <intf name>['['<expr>']']
2827 * 0: <expr.expr.expr.expr>
2828 * 1: <expr.expr.expr.expr>
2830 static int handle_yaml_config_ip2nets(struct cYAML *tree,
2831 struct cYAML **show_rc,
2832 struct cYAML **err_rc)
2834 struct cYAML *net, *ip_range, *item = NULL, *intf = NULL,
2836 struct lustre_lnet_ip2nets ip2nets;
2837 struct lustre_lnet_ip_range_descr *ip_range_descr = NULL,
2839 int rc = LUSTRE_CFG_RC_NO_ERR;
2840 struct cfs_expr_list *global_cpts = NULL;
2841 struct cfs_expr_list *el, *el_tmp;
2842 struct lnet_ioctl_config_lnd_tunables tunables;
2843 struct lnet_dlc_intf_descr *intf_descr, *intf_tmp;
2846 memset(&tunables, 0, sizeof(tunables));
2848 /* initialize all lists */
2849 INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
2850 INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
2851 INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
2853 net = cYAML_get_object_item(tree, "net-spec");
2855 return LUSTRE_CFG_RC_BAD_PARAM;
2857 if (net != NULL && net->cy_valuestring == NULL)
2858 return LUSTRE_CFG_RC_BAD_PARAM;
2860 /* assign the network id */
2861 ip2nets.ip2nets_net.nw_id = libcfs_str2net(net->cy_valuestring);
2862 if (ip2nets.ip2nets_net.nw_id == LNET_NID_ANY)
2863 return LUSTRE_CFG_RC_BAD_PARAM;
2865 seq_no = cYAML_get_object_item(tree, "seq_no");
2867 intf = cYAML_get_object_item(tree, "interfaces");
2869 rc = yaml_copy_intf_info(intf, &ip2nets.ip2nets_net);
2871 return LUSTRE_CFG_RC_BAD_PARAM;
2874 ip_range = cYAML_get_object_item(tree, "ip-range");
2875 if (ip_range != NULL) {
2876 item = ip_range->cy_child;
2877 while (item != NULL) {
2878 if (item->cy_valuestring == NULL) {
2879 item = item->cy_next;
2883 rc = lustre_lnet_add_ip_range(&ip2nets.ip2nets_ip_ranges,
2884 item->cy_valuestring);
2886 if (rc != LUSTRE_CFG_RC_NO_ERR)
2889 item = item->cy_next;
2893 found = yaml_extract_tunables(tree, &tunables, &global_cpts,
2894 LNET_NETTYP(ip2nets.ip2nets_net.nw_id));
2896 rc = lustre_lnet_config_ip2nets(&ip2nets,
2897 (found) ? &tunables : NULL,
2899 (seq_no) ? seq_no->cy_valueint : -1,
2903 * don't stop because there was no match. Continue processing the
2904 * rest of the rules. If non-match then nothing is configured
2906 if (rc == LUSTRE_CFG_RC_NO_MATCH)
2907 rc = LUSTRE_CFG_RC_NO_ERR;
2909 list_for_each_entry_safe(intf_descr, intf_tmp,
2910 &ip2nets.ip2nets_net.nw_intflist,
2912 list_del(&intf_descr->intf_on_network);
2913 free_intf_descr(intf_descr);
2916 list_for_each_entry_safe(ip_range_descr, tmp,
2917 &ip2nets.ip2nets_ip_ranges,
2919 list_del(&ip_range_descr->ipr_entry);
2920 list_for_each_entry_safe(el, el_tmp, &ip_range_descr->ipr_expr,
2922 list_del(&el->el_link);
2923 cfs_expr_list_free(el);
2925 free(ip_range_descr);
2931 static int handle_yaml_del_ni(struct cYAML *tree, struct cYAML **show_rc,
2932 struct cYAML **err_rc)
2934 struct cYAML *net = NULL, *intf = NULL, *seq_no = NULL, *item = NULL,
2936 int num_entries, rc;
2937 struct lnet_dlc_network_descr nw_descr;
2939 INIT_LIST_HEAD(&nw_descr.network_on_rule);
2940 INIT_LIST_HEAD(&nw_descr.nw_intflist);
2942 net = cYAML_get_object_item(tree, "net type");
2944 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
2946 local_nis = cYAML_get_object_item(tree, "local NI(s)");
2947 if (local_nis == NULL)
2948 return LUSTRE_CFG_RC_MISSING_PARAM;
2950 if (!cYAML_is_sequence(local_nis))
2951 return LUSTRE_CFG_RC_BAD_PARAM;
2953 while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
2954 intf = cYAML_get_object_item(item, "interfaces");
2957 num_entries = yaml_copy_intf_info(intf, &nw_descr);
2958 if (num_entries <= 0) {
2959 cYAML_build_error(num_entries, -1, "ni", "add",
2960 "bad interface list",
2962 return LUSTRE_CFG_RC_BAD_PARAM;
2966 seq_no = cYAML_get_object_item(tree, "seq_no");
2968 rc = lustre_lnet_del_ni((net) ? &nw_descr : NULL,
2969 (seq_no) ? seq_no->cy_valueint : -1,
2975 static int yaml_copy_peer_nids(struct cYAML *nids_entry, char ***nidsppp,
2976 char *prim_nid, bool del)
2978 struct cYAML *child = NULL, *entry = NULL;
2980 int num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
2982 if (cYAML_is_sequence(nids_entry)) {
2983 while (cYAML_get_next_seq_item(nids_entry, &child)) {
2984 entry = cYAML_get_object_item(child, "nid");
2985 /* don't count an empty entry */
2986 if (!entry || !entry->cy_valuestring)
2990 (strcmp(entry->cy_valuestring, prim_nid)
2993 * primary nid is present in the list of
2994 * nids so that means we want to delete
2995 * the entire peer, so no need to go
2996 * further. Just delete the entire peer.
3006 return LUSTRE_CFG_RC_MISSING_PARAM;
3008 nids = calloc(sizeof(*nids), num);
3010 return LUSTRE_CFG_RC_OUT_OF_MEM;
3012 /* now grab all the nids */
3015 while (cYAML_get_next_seq_item(nids_entry, &child)) {
3016 entry = cYAML_get_object_item(child, "nid");
3017 if (!entry || !entry->cy_valuestring)
3020 nids[num] = calloc(strlen(entry->cy_valuestring) + 1, 1);
3022 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3025 strncpy(nids[num], entry->cy_valuestring,
3026 strlen(entry->cy_valuestring));
3036 yaml_free_string_array(nids, num);
3041 static int handle_yaml_config_peer(struct cYAML *tree, struct cYAML **show_rc,
3042 struct cYAML **err_rc)
3046 struct cYAML *seq_no, *prim_nid, *non_mr, *ip2nets, *peer_nis;
3047 char err_str[LNET_MAX_STR_LEN];
3049 seq_no = cYAML_get_object_item(tree, "seq_no");
3050 prim_nid = cYAML_get_object_item(tree, "primary nid");
3051 non_mr = cYAML_get_object_item(tree, "non_mr");
3052 ip2nets = cYAML_get_object_item(tree, "ip2nets");
3053 peer_nis = cYAML_get_object_item(tree, "peer ni");
3055 if (ip2nets && (prim_nid || peer_nis)) {
3056 rc = LUSTRE_CFG_RC_BAD_PARAM;
3057 snprintf(err_str, sizeof(err_str),
3058 "ip2nets can not be specified along side prim_nid"
3059 " or peer ni fields");
3060 cYAML_build_error(rc, (seq_no) ? seq_no->cy_valueint : -1,
3061 ADD_CMD, "peer", err_str, err_rc);
3065 num = yaml_copy_peer_nids((ip2nets) ? ip2nets : peer_nis, &nids,
3066 (prim_nid) ? prim_nid->cy_valuestring : NULL,
3070 snprintf(err_str, sizeof(err_str),
3071 "error copying nids from YAML block");
3072 cYAML_build_error(num, (seq_no) ? seq_no->cy_valueint : -1,
3073 ADD_CMD, "peer", err_str, err_rc);
3077 rc = lustre_lnet_config_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
3079 (non_mr) ? false : true,
3080 (ip2nets) ? true : false,
3081 (seq_no) ? seq_no->cy_valueint : -1,
3084 yaml_free_string_array(nids, num);
3088 static int handle_yaml_del_peer(struct cYAML *tree, struct cYAML **show_rc,
3089 struct cYAML **err_rc)
3093 struct cYAML *seq_no, *prim_nid, *ip2nets, *peer_nis;
3094 char err_str[LNET_MAX_STR_LEN];
3096 seq_no = cYAML_get_object_item(tree, "seq_no");
3097 prim_nid = cYAML_get_object_item(tree, "primary nid");
3098 ip2nets = cYAML_get_object_item(tree, "ip2nets");
3099 peer_nis = cYAML_get_object_item(tree, "peer ni");
3101 if (ip2nets && (prim_nid || peer_nis)) {
3102 rc = LUSTRE_CFG_RC_BAD_PARAM;
3103 snprintf(err_str, sizeof(err_str),
3104 "ip2nets can not be specified along side prim_nid"
3105 " or peer ni fields");
3106 cYAML_build_error(rc, (seq_no) ? seq_no->cy_valueint : -1,
3107 DEL_CMD, "peer", err_str, err_rc);
3111 num = yaml_copy_peer_nids((ip2nets) ? ip2nets : peer_nis , &nids,
3112 (prim_nid) ? prim_nid->cy_valuestring : NULL,
3115 snprintf(err_str, sizeof(err_str),
3116 "error copying nids from YAML block");
3117 cYAML_build_error(num, (seq_no) ? seq_no->cy_valueint : -1,
3118 ADD_CMD, "peer", err_str, err_rc);
3122 rc = lustre_lnet_del_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
3123 nids, num, (ip2nets) ? true : false,
3124 (seq_no) ? seq_no->cy_valueint : -1,
3127 yaml_free_string_array(nids, num);
3131 static int handle_yaml_config_buffers(struct cYAML *tree,
3132 struct cYAML **show_rc,
3133 struct cYAML **err_rc)
3136 struct cYAML *tiny, *small, *large, *seq_no;
3138 tiny = cYAML_get_object_item(tree, "tiny");
3139 small = cYAML_get_object_item(tree, "small");
3140 large = cYAML_get_object_item(tree, "large");
3141 seq_no = cYAML_get_object_item(tree, "seq_no");
3143 rc = lustre_lnet_config_buffers((tiny) ? tiny->cy_valueint : -1,
3144 (small) ? small->cy_valueint : -1,
3145 (large) ? large->cy_valueint : -1,
3146 (seq_no) ? seq_no->cy_valueint : -1,
3152 static int handle_yaml_config_routing(struct cYAML *tree,
3153 struct cYAML **show_rc,
3154 struct cYAML **err_rc)
3156 int rc = LUSTRE_CFG_RC_NO_ERR;
3157 struct cYAML *seq_no, *enable;
3159 seq_no = cYAML_get_object_item(tree, "seq_no");
3160 enable = cYAML_get_object_item(tree, "enable");
3163 rc = lustre_lnet_enable_routing(enable->cy_valueint,
3165 seq_no->cy_valueint : -1,
3172 static int handle_yaml_del_route(struct cYAML *tree, struct cYAML **show_rc,
3173 struct cYAML **err_rc)
3177 struct cYAML *seq_no;
3179 net = cYAML_get_object_item(tree, "net");
3180 gw = cYAML_get_object_item(tree, "gateway");
3181 seq_no = cYAML_get_object_item(tree, "seq_no");
3183 return lustre_lnet_del_route((net) ? net->cy_valuestring : NULL,
3184 (gw) ? gw->cy_valuestring : NULL,
3185 (seq_no) ? seq_no->cy_valueint : -1,
3189 static int handle_yaml_del_routing(struct cYAML *tree, struct cYAML **show_rc,
3190 struct cYAML **err_rc)
3192 struct cYAML *seq_no;
3194 seq_no = cYAML_get_object_item(tree, "seq_no");
3196 return lustre_lnet_enable_routing(0, (seq_no) ?
3197 seq_no->cy_valueint : -1,
3201 static int handle_yaml_show_route(struct cYAML *tree, struct cYAML **show_rc,
3202 struct cYAML **err_rc)
3208 struct cYAML *detail;
3209 struct cYAML *seq_no;
3211 net = cYAML_get_object_item(tree, "net");
3212 gw = cYAML_get_object_item(tree, "gateway");
3213 hop = cYAML_get_object_item(tree, "hop");
3214 prio = cYAML_get_object_item(tree, "priority");
3215 detail = cYAML_get_object_item(tree, "detail");
3216 seq_no = cYAML_get_object_item(tree, "seq_no");
3218 return lustre_lnet_show_route((net) ? net->cy_valuestring : NULL,
3219 (gw) ? gw->cy_valuestring : NULL,
3220 (hop) ? hop->cy_valueint : -1,
3221 (prio) ? prio->cy_valueint : -1,
3222 (detail) ? detail->cy_valueint : 0,
3223 (seq_no) ? seq_no->cy_valueint : -1,
3228 static int handle_yaml_show_net(struct cYAML *tree, struct cYAML **show_rc,
3229 struct cYAML **err_rc)
3231 struct cYAML *net, *detail, *seq_no;
3233 net = cYAML_get_object_item(tree, "net");
3234 detail = cYAML_get_object_item(tree, "detail");
3235 seq_no = cYAML_get_object_item(tree, "seq_no");
3237 return lustre_lnet_show_net((net) ? net->cy_valuestring : NULL,
3238 (detail) ? detail->cy_valueint : 0,
3239 (seq_no) ? seq_no->cy_valueint : -1,
3244 static int handle_yaml_show_routing(struct cYAML *tree, struct cYAML **show_rc,
3245 struct cYAML **err_rc)
3247 struct cYAML *seq_no;
3249 seq_no = cYAML_get_object_item(tree, "seq_no");
3251 return lustre_lnet_show_routing((seq_no) ? seq_no->cy_valueint : -1,
3255 static int handle_yaml_show_peers(struct cYAML *tree, struct cYAML **show_rc,
3256 struct cYAML **err_rc)
3258 struct cYAML *seq_no, *nid, *detail;
3260 seq_no = cYAML_get_object_item(tree, "seq_no");
3261 detail = cYAML_get_object_item(tree, "detail");
3262 nid = cYAML_get_object_item(tree, "nid");
3264 return lustre_lnet_show_peer((nid) ? nid->cy_valuestring : NULL,
3265 (detail) ? detail->cy_valueint : 0,
3266 (seq_no) ? seq_no->cy_valueint : -1,
3270 static int handle_yaml_show_stats(struct cYAML *tree, struct cYAML **show_rc,
3271 struct cYAML **err_rc)
3273 struct cYAML *seq_no;
3275 seq_no = cYAML_get_object_item(tree, "seq_no");
3277 return lustre_lnet_show_stats((seq_no) ? seq_no->cy_valueint : -1,
3281 static int handle_yaml_config_numa(struct cYAML *tree, struct cYAML **show_rc,
3282 struct cYAML **err_rc)
3284 struct cYAML *seq_no, *range;
3286 seq_no = cYAML_get_object_item(tree, "seq_no");
3287 range = cYAML_get_object_item(tree, "range");
3289 return lustre_lnet_config_numa_range(range ? range->cy_valueint : -1,
3290 seq_no ? seq_no->cy_valueint : -1,
3294 static int handle_yaml_del_numa(struct cYAML *tree, struct cYAML **show_rc,
3295 struct cYAML **err_rc)
3297 struct cYAML *seq_no;
3299 seq_no = cYAML_get_object_item(tree, "seq_no");
3301 return lustre_lnet_config_numa_range(0, seq_no ? seq_no->cy_valueint : -1,
3305 static int handle_yaml_show_numa(struct cYAML *tree, struct cYAML **show_rc,
3306 struct cYAML **err_rc)
3308 struct cYAML *seq_no;
3310 seq_no = cYAML_get_object_item(tree, "seq_no");
3312 return lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint : -1,
3316 struct lookup_cmd_hdlr_tbl {
3321 static struct lookup_cmd_hdlr_tbl lookup_config_tbl[] = {
3322 { .name = "route", .cb = handle_yaml_config_route },
3323 { .name = "net", .cb = handle_yaml_config_ni },
3324 { .name = "ip2nets", .cb = handle_yaml_config_ip2nets },
3325 { .name = "peer", .cb = handle_yaml_config_peer },
3326 { .name = "routing", .cb = handle_yaml_config_routing },
3327 { .name = "buffers", .cb = handle_yaml_config_buffers },
3328 { .name = "numa", .cb = handle_yaml_config_numa },
3331 static struct lookup_cmd_hdlr_tbl lookup_del_tbl[] = {
3332 { .name = "route", .cb = handle_yaml_del_route },
3333 { .name = "net", .cb = handle_yaml_del_ni },
3334 { .name = "peer", .cb = handle_yaml_del_peer },
3335 { .name = "routing", .cb = handle_yaml_del_routing },
3336 { .name = "numa", .cb = handle_yaml_del_numa },
3339 static struct lookup_cmd_hdlr_tbl lookup_show_tbl[] = {
3340 { .name = "route", .cb = handle_yaml_show_route },
3341 { .name = "net", .cb = handle_yaml_show_net },
3342 { .name = "buffers", .cb = handle_yaml_show_routing },
3343 { .name = "routing", .cb = handle_yaml_show_routing },
3344 { .name = "peer", .cb = handle_yaml_show_peers },
3345 { .name = "statistics", .cb = handle_yaml_show_stats },
3346 { .name = "numa", .cb = handle_yaml_show_numa },
3349 static cmd_handler_t lookup_fn(char *key,
3350 struct lookup_cmd_hdlr_tbl *tbl)
3356 for (i = 0; tbl[i].name != NULL; i++) {
3357 if (strncmp(key, tbl[i].name, strlen(tbl[i].name)) == 0)
3364 static int lustre_yaml_cb_helper(char *f, struct lookup_cmd_hdlr_tbl *table,
3365 struct cYAML **show_rc, struct cYAML **err_rc)
3367 struct cYAML *tree, *item = NULL, *head, *child;
3369 char err_str[LNET_MAX_STR_LEN];
3370 int rc = LUSTRE_CFG_RC_NO_ERR, return_rc = LUSTRE_CFG_RC_NO_ERR;
3372 tree = cYAML_build_tree(f, NULL, 0, err_rc, false);
3374 return LUSTRE_CFG_RC_BAD_PARAM;
3376 child = tree->cy_child;
3377 while (child != NULL) {
3378 cb = lookup_fn(child->cy_string, table);
3380 snprintf(err_str, sizeof(err_str),
3381 "\"call back for '%s' not found\"",
3383 cYAML_build_error(LUSTRE_CFG_RC_BAD_PARAM, -1,
3384 "yaml", "helper", err_str, err_rc);
3388 if (cYAML_is_sequence(child)) {
3389 while ((head = cYAML_get_next_seq_item(child, &item))
3391 rc = cb(head, show_rc, err_rc);
3392 if (rc != LUSTRE_CFG_RC_NO_ERR)
3396 rc = cb(child, show_rc, err_rc);
3397 if (rc != LUSTRE_CFG_RC_NO_ERR)
3401 child = child->cy_next;
3405 cYAML_free_tree(tree);
3410 int lustre_yaml_config(char *f, struct cYAML **err_rc)
3412 return lustre_yaml_cb_helper(f, lookup_config_tbl,
3416 int lustre_yaml_del(char *f, struct cYAML **err_rc)
3418 return lustre_yaml_cb_helper(f, lookup_del_tbl,
3422 int lustre_yaml_show(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
3424 return lustre_yaml_cb_helper(f, lookup_show_tbl,