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, 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
38 #include <libcfs/libcfsutil.h>
39 #include <lnet/lnetctl.h>
40 #include <lnet/socklnd.h>
41 #include <lnet/lnet.h>
42 #include "liblnetconfig.h"
45 #define CONFIG_CMD "configure"
46 #define UNCONFIG_CMD "unconfigure"
49 #define SHOW_CMD "show"
51 int lustre_lnet_config_lib_init(void)
53 return register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH,
54 LNET_DEV_MAJOR, LNET_DEV_MINOR);
57 int lustre_lnet_config_ni_system(bool up, bool load_ni_from_mod,
58 int seq_no, struct cYAML **err_rc)
60 struct libcfs_ioctl_data data;
63 char err_str[LNET_MAX_STR_LEN];
65 snprintf(err_str, sizeof(err_str), "\"Success\"");
67 LIBCFS_IOC_INIT(data);
69 /* Reverse logic is used here in order not to change
71 data.ioc_flags = load_ni_from_mod ? 0 : 1;
73 opc = up ? IOC_LIBCFS_CONFIGURE : IOC_LIBCFS_UNCONFIGURE;
75 rc = l_ioctl(LNET_DEV_ID, opc, &data);
80 "\"LNet %s error: %s\"", (up) ? "configure" :
81 "unconfigure", strerror(errno));
85 cYAML_build_error(rc, seq_no, (up) ? CONFIG_CMD : UNCONFIG_CMD,
86 "lnet", err_str, err_rc);
91 int lustre_lnet_config_route(char *nw, char *gw, int hops, int prio,
92 int seq_no, struct cYAML **err_rc)
94 struct lnet_ioctl_config_data data;
95 lnet_nid_t gateway_nid;
96 int rc = LUSTRE_CFG_RC_NO_ERR;
97 __u32 net = LNET_NIDNET(LNET_NID_ANY);
98 char err_str[LNET_MAX_STR_LEN];
100 snprintf(err_str, sizeof(err_str), "\"Success\"");
102 if (nw == NULL || gw == NULL) {
105 "\"missing mandatory parameter(s): '%s'\"",
106 (nw == NULL && gw == NULL) ? "network, gateway" :
107 (nw == NULL) ? "network" : "gateway");
108 rc = LUSTRE_CFG_RC_MISSING_PARAM;
112 net = libcfs_str2net(nw);
113 if (net == LNET_NIDNET(LNET_NID_ANY)) {
116 "\"cannot parse net %s\"", nw);
117 rc = LUSTRE_CFG_RC_BAD_PARAM;
121 if (LNET_NETTYP(net) == CIBLND ||
122 LNET_NETTYP(net) == OPENIBLND ||
123 LNET_NETTYP(net) == IIBLND ||
124 LNET_NETTYP(net) == VIBLND) {
127 "\"obselete LNet type '%s'\"", libcfs_lnd2str(net));
128 rc = LUSTRE_CFG_RC_BAD_PARAM;
132 gateway_nid = libcfs_str2nid(gw);
133 if (gateway_nid == LNET_NID_ANY) {
136 "\"cannot parse gateway NID '%s'\"", gw);
137 rc = LUSTRE_CFG_RC_BAD_PARAM;
142 /* -1 indicates to use the default hop value */
144 } else if (hops < 1 || hops > 255) {
147 "\"invalid hop count %d, must be between 0 and 256\"",
149 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
155 } else if (prio < 0) {
158 "\"invalid priority %d, must be greater than 0\"",
160 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
164 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
166 data.cfg_config_u.cfg_route.rtr_hop = hops;
167 data.cfg_config_u.cfg_route.rtr_priority = prio;
168 data.cfg_nid = gateway_nid;
170 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
174 "\"cannot add route: %s\"", strerror(errno));
180 cYAML_build_error(rc, seq_no, ADD_CMD, "route", err_str, err_rc);
185 int lustre_lnet_del_route(char *nw, char *gw,
186 int seq_no, struct cYAML **err_rc)
188 struct lnet_ioctl_config_data data;
189 lnet_nid_t gateway_nid;
190 int rc = LUSTRE_CFG_RC_NO_ERR;
191 __u32 net = LNET_NIDNET(LNET_NID_ANY);
192 char err_str[LNET_MAX_STR_LEN];
194 snprintf(err_str, sizeof(err_str), "\"Success\"");
196 if (nw == NULL || gw == NULL) {
199 "\"missing mandatory parameter(s): '%s'\"",
200 (nw == NULL && gw == NULL) ? "network, gateway" :
201 (nw == NULL) ? "network" : "gateway");
202 rc = LUSTRE_CFG_RC_MISSING_PARAM;
206 net = libcfs_str2net(nw);
207 if (net == LNET_NIDNET(LNET_NID_ANY)) {
210 "\"cannot parse net '%s'\"", nw);
211 rc = LUSTRE_CFG_RC_BAD_PARAM;
215 if (LNET_NETTYP(net) == CIBLND ||
216 LNET_NETTYP(net) == OPENIBLND ||
217 LNET_NETTYP(net) == IIBLND ||
218 LNET_NETTYP(net) == VIBLND) {
221 "\"obselete LNet type '%s'\"", libcfs_lnd2str(net));
222 rc = LUSTRE_CFG_RC_BAD_PARAM;
226 gateway_nid = libcfs_str2nid(gw);
227 if (gateway_nid == LNET_NID_ANY) {
230 "\"cannot parse gateway NID '%s'\"", gw);
231 rc = LUSTRE_CFG_RC_BAD_PARAM;
235 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
237 data.cfg_nid = gateway_nid;
239 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
243 "\"cannot delete route: %s\"", strerror(errno));
249 cYAML_build_error(rc, seq_no, DEL_CMD, "route", err_str, err_rc);
254 int lustre_lnet_show_route(char *nw, char *gw, int hops, int prio, int detail,
255 int seq_no, struct cYAML **show_rc,
256 struct cYAML **err_rc)
258 struct lnet_ioctl_config_data data;
259 lnet_nid_t gateway_nid;
260 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
261 __u32 net = LNET_NIDNET(LNET_NID_ANY);
263 struct cYAML *root = NULL, *route = NULL, *item = NULL;
264 struct cYAML *first_seq = NULL;
265 char err_str[LNET_MAX_STR_LEN];
268 snprintf(err_str, sizeof(err_str),
269 "\"out of memory\"");
272 net = libcfs_str2net(nw);
273 if (net == LNET_NIDNET(LNET_NID_ANY)) {
276 "\"cannot parse net '%s'\"", nw);
277 rc = LUSTRE_CFG_RC_BAD_PARAM;
281 if (LNET_NETTYP(net) == CIBLND ||
282 LNET_NETTYP(net) == OPENIBLND ||
283 LNET_NETTYP(net) == IIBLND ||
284 LNET_NETTYP(net) == VIBLND) {
287 "\"obsolete LNet type '%s'\"",
288 libcfs_lnd2str(net));
289 rc = LUSTRE_CFG_RC_BAD_PARAM;
293 /* show all routes without filtering on net */
294 net = LNET_NIDNET(LNET_NID_ANY);
298 gateway_nid = libcfs_str2nid(gw);
299 if (gateway_nid == LNET_NID_ANY) {
302 "\"cannot parse gateway NID '%s'\"", gw);
303 rc = LUSTRE_CFG_RC_BAD_PARAM;
307 /* show all routes with out filtering on gateway */
308 gateway_nid = LNET_NID_ANY;
310 if ((hops < 1 && hops != -1) || hops > 255) {
313 "\"invalid hop count %d, must be between 0 and 256\"",
315 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
319 /* create struct cYAML root object */
320 root = cYAML_create_object(NULL, NULL);
324 route = cYAML_create_seq(root, "route");
329 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
332 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
336 /* filter on provided data */
337 if (net != LNET_NIDNET(LNET_NID_ANY) &&
341 if (gateway_nid != LNET_NID_ANY &&
342 gateway_nid != data.cfg_nid)
346 hops != data.cfg_config_u.cfg_route.rtr_hop)
350 prio != data.cfg_config_u.cfg_route.rtr_priority)
353 /* default rc to -1 incase we hit the goto */
357 item = cYAML_create_seq_item(route);
361 if (first_seq == NULL)
364 if (cYAML_create_string(item, "net",
365 libcfs_net2str(data.cfg_net)) == NULL)
368 if (cYAML_create_string(item, "gateway",
369 libcfs_nid2str(data.cfg_nid)) == NULL)
373 if (cYAML_create_number(item, "hop",
374 data.cfg_config_u.cfg_route.
379 if (cYAML_create_number(item, "priority",
381 cfg_route.rtr_priority) == NULL)
384 if (cYAML_create_string(item, "state",
385 data.cfg_config_u.cfg_route.
387 "up" : "down") == NULL)
392 /* print output iff show_rc is not provided */
394 cYAML_print_tree(root);
396 if (errno != ENOENT) {
399 "\"cannot get routes: %s\"",
404 rc = LUSTRE_CFG_RC_NO_ERR;
406 snprintf(err_str, sizeof(err_str), "\"success\"");
408 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
409 cYAML_free_tree(root);
410 } else if (show_rc != NULL && *show_rc != NULL) {
411 struct cYAML *show_node;
412 /* find the route node, if one doesn't exist then
413 * insert one. Otherwise add to the one there
415 show_node = cYAML_get_object_item(*show_rc, "route");
416 if (show_node != NULL && cYAML_is_sequence(show_node)) {
417 cYAML_insert_child(show_node, first_seq);
420 } else if (show_node == NULL) {
421 cYAML_insert_sibling((*show_rc)->cy_child,
425 cYAML_free_tree(root);
431 cYAML_build_error(rc, seq_no, SHOW_CMD, "route", err_str, err_rc);
436 int lustre_lnet_config_net(char *net, char *intf, char *ip2net,
437 int peer_to, int peer_cr, int peer_buf_cr,
438 int credits, char *smp, int seq_no,
439 struct cYAML **err_rc)
441 struct lnet_ioctl_config_data data;
442 char buf[LNET_MAX_STR_LEN];
443 int rc = LUSTRE_CFG_RC_NO_ERR, num_of_nets = 0;
444 char err_str[LNET_MAX_STR_LEN];
446 snprintf(err_str, sizeof(err_str), "\"success\"");
448 if (ip2net == NULL && (intf == NULL || net == NULL)) {
451 "\"mandatory parameter '%s' not specified."
452 " Optionally specify ip2net parameter\"",
453 (intf == NULL && net == NULL) ? "net, if" :
454 (intf == NULL) ? "if" : "net");
455 rc = LUSTRE_CFG_RC_MISSING_PARAM;
459 if (peer_to != -1 && peer_to <= 0) {
462 "\"peer timeout %d, must be greater than 0\"",
464 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
468 if (ip2net != NULL && strlen(ip2net) >= sizeof(buf)) {
471 "\"ip2net string too long %d\"",
472 (int)strlen(ip2net));
473 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
478 snprintf(buf, sizeof(buf) - 1, "%s(%s)%s",
482 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
483 strncpy(data.cfg_config_u.cfg_net.net_intf,
484 (ip2net != NULL) ? ip2net : buf, sizeof(buf));
485 data.cfg_config_u.cfg_net.net_peer_timeout = peer_to;
486 data.cfg_config_u.cfg_net.net_peer_tx_credits = peer_cr;
487 data.cfg_config_u.cfg_net.net_peer_rtr_credits = peer_buf_cr;
488 data.cfg_config_u.cfg_net.net_max_tx_credits = credits;
490 num_of_nets = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_NET, &data);
491 if (num_of_nets < 0) {
494 "\"cannot add network: %s\"", strerror(errno));
499 cYAML_build_error((num_of_nets > 0) ? num_of_nets : rc,
500 seq_no, ADD_CMD, "net", err_str, err_rc);
505 int lustre_lnet_del_net(char *nw, int seq_no, struct cYAML **err_rc)
507 struct lnet_ioctl_config_data data;
508 __u32 net = LNET_NIDNET(LNET_NID_ANY);
509 int rc = LUSTRE_CFG_RC_NO_ERR;
510 char err_str[LNET_MAX_STR_LEN];
512 snprintf(err_str, sizeof(err_str), "\"success\"");
517 "\"missing mandatory parameter\"");
518 rc = LUSTRE_CFG_RC_MISSING_PARAM;
522 net = libcfs_str2net(nw);
523 if (net == LNET_NIDNET(LNET_NID_ANY)) {
526 "\"cannot parse net '%s'\"", nw);
527 rc = LUSTRE_CFG_RC_BAD_PARAM;
531 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
534 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_NET, &data);
538 "\"cannot delete network: %s\"", strerror(errno));
544 cYAML_build_error(rc, seq_no, DEL_CMD, "net", err_str, err_rc);
549 int lustre_lnet_show_net(char *nw, int detail, int seq_no,
550 struct cYAML **show_rc, struct cYAML **err_rc)
553 struct lnet_ioctl_config_data *data;
554 struct lnet_ioctl_net_config *net_config;
555 __u32 net = LNET_NIDNET(LNET_NID_ANY);
556 int rc = LUSTRE_CFG_RC_OUT_OF_MEM, i, j;
557 struct cYAML *root = NULL, *tunables = NULL,
558 *net_node = NULL, *interfaces = NULL,
559 *item = NULL, *first_seq = NULL;
560 int str_buf_len = LNET_MAX_SHOW_NUM_CPT * 2;
561 char str_buf[str_buf_len];
563 char err_str[LNET_MAX_STR_LEN];
566 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
568 buf = calloc(1, sizeof(*data) + sizeof(*net_config));
572 data = (struct lnet_ioctl_config_data *)buf;
575 net = libcfs_str2net(nw);
576 if (net == LNET_NIDNET(LNET_NID_ANY)) {
579 "\"cannot parse net '%s'\"", nw);
580 rc = LUSTRE_CFG_RC_BAD_PARAM;
585 root = cYAML_create_object(NULL, NULL);
589 net_node = cYAML_create_seq(root, "net");
590 if (net_node == NULL)
596 memset(buf, 0, sizeof(*data) + sizeof(*net_config));
598 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
600 * set the ioc_len to the proper value since INIT assumes
603 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
604 sizeof(struct lnet_ioctl_net_config);
607 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NET, data);
611 /* filter on provided data */
612 if (net != LNET_NIDNET(LNET_NID_ANY) &&
613 net != LNET_NIDNET(data->cfg_nid))
616 /* default rc to -1 in case we hit the goto */
620 net_config = (struct lnet_ioctl_net_config *)data->cfg_bulk;
622 /* create the tree to be printed. */
623 item = cYAML_create_seq_item(net_node);
627 if (first_seq == NULL)
630 if (cYAML_create_string(item,
633 LNET_NIDNET(data->cfg_nid)))
637 if (cYAML_create_string(item, "nid",
638 libcfs_nid2str(data->cfg_nid)) == NULL)
641 if (cYAML_create_string(item,
643 (net_config->ni_status ==
645 "up" : "down") == NULL)
648 /* don't add interfaces unless there is at least one
650 if (strlen(net_config->ni_interfaces[0]) > 0) {
651 interfaces = cYAML_create_object(item, "interfaces");
652 if (interfaces == NULL)
655 for (j = 0; j < LNET_MAX_INTERFACES; j++) {
656 if (strlen(net_config->ni_interfaces[j]) > 0) {
658 sizeof(str_buf), "%d", j);
659 if (cYAML_create_string(interfaces,
661 net_config->ni_interfaces[j]) ==
671 tunables = cYAML_create_object(item, "tunables");
672 if (tunables == NULL)
675 if (cYAML_create_number(tunables, "peer_timeout",
676 data->cfg_config_u.cfg_net.
677 net_peer_timeout) == NULL)
680 if (cYAML_create_number(tunables, "peer_credits",
681 data->cfg_config_u.cfg_net.
682 net_peer_tx_credits) == NULL)
685 if (cYAML_create_number(tunables,
686 "peer_buffer_credits",
687 data->cfg_config_u.cfg_net.
688 net_peer_rtr_credits) == NULL)
691 if (cYAML_create_number(tunables, "credits",
692 data->cfg_config_u.cfg_net.
693 net_max_tx_credits) == NULL)
696 /* out put the CPTs in the format: "[x,x,x,...]" */
697 limit = str_buf + str_buf_len - 3;
698 pos += snprintf(pos, limit - pos, "\"[");
699 for (j = 0 ; data->cfg_ncpts > 1 &&
700 j < data->cfg_ncpts &&
702 pos += snprintf(pos, limit - pos,
703 "%d", net_config->ni_cpts[j]);
704 if ((j + 1) < data->cfg_ncpts)
705 pos += snprintf(pos, limit - pos, ",");
707 pos += snprintf(pos, 3, "]\"");
709 if (data->cfg_ncpts > 1 &&
710 cYAML_create_string(tunables, "CPT",
716 /* Print out the net information only if show_rc is not provided */
718 cYAML_print_tree(root);
720 if (errno != ENOENT) {
723 "\"cannot get networks: %s\"",
728 rc = LUSTRE_CFG_RC_NO_ERR;
730 snprintf(err_str, sizeof(err_str), "\"success\"");
732 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
733 cYAML_free_tree(root);
734 } else if (show_rc != NULL && *show_rc != NULL) {
735 struct cYAML *show_node;
736 /* find the net node, if one doesn't exist
737 * then insert one. Otherwise add to the one there
739 show_node = cYAML_get_object_item(*show_rc, "net");
740 if (show_node != NULL && cYAML_is_sequence(show_node)) {
741 cYAML_insert_child(show_node, first_seq);
744 } else if (show_node == NULL) {
745 cYAML_insert_sibling((*show_rc)->cy_child,
749 cYAML_free_tree(root);
755 cYAML_build_error(rc, seq_no, SHOW_CMD, "net", err_str, err_rc);
760 int lustre_lnet_enable_routing(int enable, int seq_no, struct cYAML **err_rc)
762 struct lnet_ioctl_config_data data;
763 int rc = LUSTRE_CFG_RC_NO_ERR;
764 char err_str[LNET_MAX_STR_LEN];
766 snprintf(err_str, sizeof(err_str), "\"success\"");
768 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
769 data.cfg_config_u.cfg_buffers.buf_enable = (enable) ? 1 : 0;
771 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIG_RTR, &data);
775 "\"cannot %s routing %s\"",
776 (enable) ? "enable" : "disable", strerror(errno));
782 cYAML_build_error(rc, seq_no,
783 (enable) ? ADD_CMD : DEL_CMD,
784 "routing", err_str, err_rc);
789 int lustre_lnet_config_buffers(int tiny, int small, int large, int seq_no,
790 struct cYAML **err_rc)
792 struct lnet_ioctl_config_data data;
793 int rc = LUSTRE_CFG_RC_NO_ERR;
794 char err_str[LNET_MAX_STR_LEN];
796 snprintf(err_str, sizeof(err_str), "\"success\"");
798 /* -1 indicates to ignore changes to this field */
799 if (tiny < -1 || small < -1 || large < -1) {
802 "\"tiny, small and large must be >= 0\"");
803 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
807 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
808 data.cfg_config_u.cfg_buffers.buf_tiny = tiny;
809 data.cfg_config_u.cfg_buffers.buf_small = small;
810 data.cfg_config_u.cfg_buffers.buf_large = large;
812 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_BUF, &data);
816 "\"cannot configure buffers: %s\"", strerror(errno));
822 cYAML_build_error(rc, seq_no, DEL_CMD, "buf", err_str, err_rc);
827 int lustre_lnet_show_routing(int seq_no, struct cYAML **show_rc,
828 struct cYAML **err_rc)
830 struct lnet_ioctl_config_data *data;
831 struct lnet_ioctl_pool_cfg *pool_cfg = NULL;
832 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
834 char *pools[LNET_NRBPOOLS] = {"tiny", "small", "large"};
835 struct cYAML *root = NULL, *pools_node = NULL,
836 *type_node = NULL, *item = NULL, *cpt = NULL,
839 char err_str[LNET_MAX_STR_LEN];
840 char node_name[LNET_MAX_STR_LEN];
843 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
845 buf = calloc(1, sizeof(*data) + sizeof(*pool_cfg));
849 data = (struct lnet_ioctl_config_data *)buf;
851 root = cYAML_create_object(NULL, NULL);
855 pools_node = cYAML_create_seq(root, "routing");
856 if (pools_node == NULL)
860 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
861 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
862 sizeof(struct lnet_ioctl_pool_cfg);
865 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_BUF, data);
871 pool_cfg = (struct lnet_ioctl_pool_cfg *)data->cfg_bulk;
873 snprintf(node_name, sizeof(node_name), "cpt[%d]", i);
874 item = cYAML_create_seq_item(pools_node);
878 if (first_seq == NULL)
881 cpt = cYAML_create_object(item, node_name);
885 /* create the tree and print */
886 for (j = 0; j < LNET_NRBPOOLS; j++) {
887 type_node = cYAML_create_object(cpt, pools[j]);
888 if (type_node == NULL)
890 if (cYAML_create_number(type_node, "npages",
891 pool_cfg->pl_pools[j].pl_npages)
894 if (cYAML_create_number(type_node, "nbuffers",
895 pool_cfg->pl_pools[j].
896 pl_nbuffers) == NULL)
898 if (cYAML_create_number(type_node, "credits",
899 pool_cfg->pl_pools[j].
902 if (cYAML_create_number(type_node, "mincredits",
903 pool_cfg->pl_pools[j].
904 pl_mincredits) == NULL)
909 if (pool_cfg != NULL) {
910 item = cYAML_create_seq_item(pools_node);
914 if (cYAML_create_number(item, "enable", pool_cfg->pl_routing) ==
920 cYAML_print_tree(root);
922 if (errno != ENOENT) {
925 "\"cannot get routing information: %s\"",
930 rc = LUSTRE_CFG_RC_NO_ERR;
932 snprintf(err_str, sizeof(err_str), "\"success\"");
933 rc = LUSTRE_CFG_RC_NO_ERR;
937 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
938 cYAML_free_tree(root);
939 } else if (show_rc != NULL && *show_rc != NULL) {
940 struct cYAML *show_node;
941 /* find the routing node, if one doesn't exist then
942 * insert one. Otherwise add to the one there
944 show_node = cYAML_get_object_item(*show_rc, "routing");
945 if (show_node != NULL && cYAML_is_sequence(show_node)) {
946 cYAML_insert_child(show_node, first_seq);
949 } else if (show_node == NULL) {
950 cYAML_insert_sibling((*show_rc)->cy_child,
954 cYAML_free_tree(root);
960 cYAML_build_error(rc, seq_no, SHOW_CMD, "routing", err_str, err_rc);
965 int lustre_lnet_show_peer_credits(int seq_no, struct cYAML **show_rc,
966 struct cYAML **err_rc)
968 struct lnet_ioctl_peer peer_info;
969 int rc = LUSTRE_CFG_RC_OUT_OF_MEM, ncpt = 0, i = 0, j = 0;
970 struct cYAML *root = NULL, *peer = NULL, *first_seq = NULL,
972 char err_str[LNET_MAX_STR_LEN];
973 bool ncpt_set = false;
975 snprintf(err_str, sizeof(err_str),
976 "\"out of memory\"");
978 /* create struct cYAML root object */
979 root = cYAML_create_object(NULL, NULL);
983 peer_root = cYAML_create_seq(root, "peer");
984 if (peer_root == NULL)
989 LIBCFS_IOC_INIT_V2(peer_info, pr_hdr);
990 peer_info.pr_count = i;
991 peer_info.pr_lnd_u.pr_peer_credits.cr_ncpt = j;
992 rc = l_ioctl(LNET_DEV_ID,
993 IOC_LIBCFS_GET_PEER_INFO, &peer_info);
998 ncpt = peer_info.pr_lnd_u.pr_peer_credits.
1003 peer = cYAML_create_seq_item(peer_root);
1007 if (first_seq == NULL)
1010 if (cYAML_create_string(peer, "nid",
1012 (peer_info.pr_nid)) == NULL)
1015 if (cYAML_create_string(peer, "state",
1022 if (cYAML_create_number(peer, "refcount",
1025 cr_refcount) == NULL)
1028 if (cYAML_create_number(peer, "max_ni_tx_credits",
1031 cr_ni_peer_tx_credits)
1035 if (cYAML_create_number(peer, "available_tx_credits",
1042 if (cYAML_create_number(peer, "available_rtr_credits",
1045 cr_peer_rtr_credits)
1049 if (cYAML_create_number(peer, "min_rtr_credits",
1052 cr_peer_min_rtr_credits)
1056 if (cYAML_create_number(peer, "tx_q_num_of_buf",
1064 if (errno != ENOENT) {
1067 "\"cannot get peer information: %s\"",
1076 /* print output iff show_rc is not provided */
1077 if (show_rc == NULL)
1078 cYAML_print_tree(root);
1080 snprintf(err_str, sizeof(err_str), "\"success\"");
1081 rc = LUSTRE_CFG_RC_NO_ERR;
1084 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
1085 cYAML_free_tree(root);
1086 } else if (show_rc != NULL && *show_rc != NULL) {
1087 struct cYAML *show_node;
1088 /* find the peer node, if one doesn't exist then
1089 * insert one. Otherwise add to the one there
1091 show_node = cYAML_get_object_item(*show_rc,
1093 if (show_node != NULL && cYAML_is_sequence(show_node)) {
1094 cYAML_insert_child(show_node, first_seq);
1097 } else if (show_node == NULL) {
1098 cYAML_insert_sibling((*show_rc)->cy_child,
1102 cYAML_free_tree(root);
1108 cYAML_build_error(rc, seq_no, SHOW_CMD, "peer_credits", err_str,
1114 int lustre_lnet_show_stats(int seq_no, struct cYAML **show_rc,
1115 struct cYAML **err_rc)
1117 struct lnet_ioctl_lnet_stats data;
1118 int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1119 char err_str[LNET_MAX_STR_LEN];
1120 struct cYAML *root = NULL, *stats = NULL;
1122 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1124 LIBCFS_IOC_INIT_V2(data, st_hdr);
1126 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LNET_STATS, &data);
1130 "\"cannot get lnet statistics: %s\"",
1136 root = cYAML_create_object(NULL, NULL);
1140 stats = cYAML_create_object(root, "statistics");
1144 if (cYAML_create_number(stats, "msgs_alloc",
1145 data.st_cntrs.msgs_alloc) == NULL)
1148 if (cYAML_create_number(stats, "msgs_max",
1149 data.st_cntrs.msgs_max) == NULL)
1152 if (cYAML_create_number(stats, "errors",
1153 data.st_cntrs.errors) == NULL)
1156 if (cYAML_create_number(stats, "send_count",
1157 data.st_cntrs.send_count) == NULL)
1160 if (cYAML_create_number(stats, "recv_count",
1161 data.st_cntrs.recv_count) == NULL)
1164 if (cYAML_create_number(stats, "route_count",
1165 data.st_cntrs.route_count) == NULL)
1168 if (cYAML_create_number(stats, "drop_count",
1169 data.st_cntrs.drop_count) == NULL)
1172 if (cYAML_create_number(stats, "send_length",
1173 data.st_cntrs.send_length) == NULL)
1176 if (cYAML_create_number(stats, "recv_length",
1177 data.st_cntrs.recv_length) == NULL)
1180 if (cYAML_create_number(stats, "route_length",
1181 data.st_cntrs.route_length) == NULL)
1184 if (cYAML_create_number(stats, "drop_length",
1185 data.st_cntrs.drop_length) == NULL)
1188 if (show_rc == NULL)
1189 cYAML_print_tree(root);
1191 snprintf(err_str, sizeof(err_str), "\"success\"");
1193 if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
1194 cYAML_free_tree(root);
1195 } else if (show_rc != NULL && *show_rc != NULL) {
1196 cYAML_insert_sibling((*show_rc)->cy_child,
1203 cYAML_build_error(rc, seq_no, SHOW_CMD, "statistics", err_str, err_rc);
1208 typedef int (*cmd_handler_t)(struct cYAML *tree,
1209 struct cYAML **show_rc,
1210 struct cYAML **err_rc);
1212 static int handle_yaml_config_route(struct cYAML *tree, struct cYAML **show_rc,
1213 struct cYAML **err_rc)
1215 struct cYAML *net, *gw, *hop, *prio, *seq_no;
1217 net = cYAML_get_object_item(tree, "net");
1218 gw = cYAML_get_object_item(tree, "gateway");
1219 hop = cYAML_get_object_item(tree, "hop");
1220 prio = cYAML_get_object_item(tree, "priority");
1221 seq_no = cYAML_get_object_item(tree, "seq_no");
1223 return lustre_lnet_config_route((net) ? net->cy_valuestring : NULL,
1224 (gw) ? gw->cy_valuestring : NULL,
1225 (hop) ? hop->cy_valueint : -1,
1226 (prio) ? prio->cy_valueint : -1,
1227 (seq_no) ? seq_no->cy_valueint : -1,
1231 static int handle_yaml_config_net(struct cYAML *tree, struct cYAML **show_rc,
1232 struct cYAML **err_rc)
1234 struct cYAML *net, *intf, *tunables, *seq_no,
1235 *peer_to = NULL, *peer_buf_cr = NULL, *peer_cr = NULL,
1236 *credits = NULL, *ip2net = NULL, *smp = NULL, *child;
1237 char devs[LNET_MAX_STR_LEN];
1239 int size = LNET_MAX_STR_LEN;
1241 bool intf_found = false;
1243 ip2net = cYAML_get_object_item(tree, "ip2net");
1244 net = cYAML_get_object_item(tree, "net");
1245 intf = cYAML_get_object_item(tree, "interfaces");
1247 /* grab all the interfaces */
1248 child = intf->cy_child;
1249 while (child != NULL && size > 0) {
1251 num = snprintf(loc, size, ",%s",
1252 child->cy_valuestring);
1254 num = snprintf(loc, size, "%s",
1255 child->cy_valuestring);
1259 child = child->cy_next;
1263 tunables = cYAML_get_object_item(tree, "tunables");
1264 if (tunables != NULL) {
1265 peer_to = cYAML_get_object_item(tunables, "peer_timeout");
1266 peer_cr = cYAML_get_object_item(tunables, "peer_credits");
1267 peer_buf_cr = cYAML_get_object_item(tunables,
1268 "peer_buffer_credits");
1269 credits = cYAML_get_object_item(tunables, "credits");
1270 smp = cYAML_get_object_item(tunables, "CPT");
1272 seq_no = cYAML_get_object_item(tree, "seq_no");
1274 return lustre_lnet_config_net((net) ? net->cy_valuestring : NULL,
1275 (intf_found) ? devs : NULL,
1276 (ip2net) ? ip2net->cy_valuestring : NULL,
1277 (peer_to) ? peer_to->cy_valueint : -1,
1278 (peer_cr) ? peer_cr->cy_valueint : -1,
1280 peer_buf_cr->cy_valueint : -1,
1281 (credits) ? credits->cy_valueint : -1,
1282 (smp) ? smp->cy_valuestring : NULL,
1283 (seq_no) ? seq_no->cy_valueint : -1,
1287 static int handle_yaml_config_buffers(struct cYAML *tree,
1288 struct cYAML **show_rc,
1289 struct cYAML **err_rc)
1292 struct cYAML *tiny, *small, *large, *seq_no;
1294 tiny = cYAML_get_object_item(tree, "tiny");
1295 small = cYAML_get_object_item(tree, "small");
1296 large = cYAML_get_object_item(tree, "large");
1297 seq_no = cYAML_get_object_item(tree, "seq_no");
1299 rc = lustre_lnet_config_buffers((tiny) ? tiny->cy_valueint : -1,
1300 (small) ? small->cy_valueint : -1,
1301 (large) ? large->cy_valueint : -1,
1302 (seq_no) ? seq_no->cy_valueint : -1,
1308 static int handle_yaml_config_routing(struct cYAML *tree,
1309 struct cYAML **show_rc,
1310 struct cYAML **err_rc)
1312 int rc = LUSTRE_CFG_RC_NO_ERR;
1313 struct cYAML *seq_no, *enable;
1315 seq_no = cYAML_get_object_item(tree, "seq_no");
1316 enable = cYAML_get_object_item(tree, "enable");
1319 rc = lustre_lnet_enable_routing(enable->cy_valueint,
1321 seq_no->cy_valueint : -1,
1328 static int handle_yaml_del_route(struct cYAML *tree, struct cYAML **show_rc,
1329 struct cYAML **err_rc)
1333 struct cYAML *seq_no;
1335 net = cYAML_get_object_item(tree, "net");
1336 gw = cYAML_get_object_item(tree, "gateway");
1337 seq_no = cYAML_get_object_item(tree, "seq_no");
1339 return lustre_lnet_del_route((net) ? net->cy_valuestring : NULL,
1340 (gw) ? gw->cy_valuestring : NULL,
1341 (seq_no) ? seq_no->cy_valueint : -1,
1345 static int handle_yaml_del_net(struct cYAML *tree, struct cYAML **show_rc,
1346 struct cYAML **err_rc)
1348 struct cYAML *net, *seq_no;
1350 net = cYAML_get_object_item(tree, "net");
1351 seq_no = cYAML_get_object_item(tree, "seq_no");
1353 return lustre_lnet_del_net((net) ? net->cy_valuestring : NULL,
1354 (seq_no) ? seq_no->cy_valueint : -1,
1358 static int handle_yaml_del_routing(struct cYAML *tree, struct cYAML **show_rc,
1359 struct cYAML **err_rc)
1361 struct cYAML *seq_no;
1363 seq_no = cYAML_get_object_item(tree, "seq_no");
1365 return lustre_lnet_enable_routing(0, (seq_no) ?
1366 seq_no->cy_valueint : -1,
1370 static int handle_yaml_show_route(struct cYAML *tree, struct cYAML **show_rc,
1371 struct cYAML **err_rc)
1377 struct cYAML *detail;
1378 struct cYAML *seq_no;
1380 net = cYAML_get_object_item(tree, "net");
1381 gw = cYAML_get_object_item(tree, "gateway");
1382 hop = cYAML_get_object_item(tree, "hop");
1383 prio = cYAML_get_object_item(tree, "priority");
1384 detail = cYAML_get_object_item(tree, "detail");
1385 seq_no = cYAML_get_object_item(tree, "seq_no");
1387 return lustre_lnet_show_route((net) ? net->cy_valuestring : NULL,
1388 (gw) ? gw->cy_valuestring : NULL,
1389 (hop) ? hop->cy_valueint : -1,
1390 (prio) ? prio->cy_valueint : -1,
1391 (detail) ? detail->cy_valueint : 0,
1392 (seq_no) ? seq_no->cy_valueint : -1,
1397 static int handle_yaml_show_net(struct cYAML *tree, struct cYAML **show_rc,
1398 struct cYAML **err_rc)
1400 struct cYAML *net, *detail, *seq_no;
1402 net = cYAML_get_object_item(tree, "net");
1403 detail = cYAML_get_object_item(tree, "detail");
1404 seq_no = cYAML_get_object_item(tree, "seq_no");
1406 return lustre_lnet_show_net((net) ? net->cy_valuestring : NULL,
1407 (detail) ? detail->cy_valueint : 0,
1408 (seq_no) ? seq_no->cy_valueint : -1,
1413 static int handle_yaml_show_routing(struct cYAML *tree, struct cYAML **show_rc,
1414 struct cYAML **err_rc)
1416 struct cYAML *seq_no;
1418 seq_no = cYAML_get_object_item(tree, "seq_no");
1420 return lustre_lnet_show_routing((seq_no) ? seq_no->cy_valueint : -1,
1424 static int handle_yaml_show_credits(struct cYAML *tree, struct cYAML **show_rc,
1425 struct cYAML **err_rc)
1427 struct cYAML *seq_no;
1429 seq_no = cYAML_get_object_item(tree, "seq_no");
1431 return lustre_lnet_show_peer_credits((seq_no) ?
1432 seq_no->cy_valueint : -1,
1436 static int handle_yaml_show_stats(struct cYAML *tree, struct cYAML **show_rc,
1437 struct cYAML **err_rc)
1439 struct cYAML *seq_no;
1441 seq_no = cYAML_get_object_item(tree, "seq_no");
1443 return lustre_lnet_show_stats((seq_no) ? seq_no->cy_valueint : -1,
1447 struct lookup_cmd_hdlr_tbl {
1452 static struct lookup_cmd_hdlr_tbl lookup_config_tbl[] = {
1453 {"route", handle_yaml_config_route},
1454 {"net", handle_yaml_config_net},
1455 {"routing", handle_yaml_config_routing},
1456 {"buffers", handle_yaml_config_buffers},
1460 static struct lookup_cmd_hdlr_tbl lookup_del_tbl[] = {
1461 {"route", handle_yaml_del_route},
1462 {"net", handle_yaml_del_net},
1463 {"routing", handle_yaml_del_routing},
1467 static struct lookup_cmd_hdlr_tbl lookup_show_tbl[] = {
1468 {"route", handle_yaml_show_route},
1469 {"net", handle_yaml_show_net},
1470 {"buffers", handle_yaml_show_routing},
1471 {"routing", handle_yaml_show_routing},
1472 {"credits", handle_yaml_show_credits},
1473 {"statistics", handle_yaml_show_stats},
1477 static cmd_handler_t lookup_fn(char *key,
1478 struct lookup_cmd_hdlr_tbl *tbl)
1484 for (i = 0; tbl[i].name != NULL; i++) {
1485 if (strncmp(key, tbl[i].name, strlen(tbl[i].name)) == 0)
1492 static int lustre_yaml_cb_helper(char *f, struct lookup_cmd_hdlr_tbl *table,
1493 struct cYAML **show_rc, struct cYAML **err_rc)
1495 struct cYAML *tree, *item = NULL, *head, *child;
1497 char err_str[LNET_MAX_STR_LEN];
1498 int rc = LUSTRE_CFG_RC_NO_ERR, return_rc = LUSTRE_CFG_RC_NO_ERR;
1500 tree = cYAML_build_tree(f, NULL, 0, err_rc);
1502 return LUSTRE_CFG_RC_BAD_PARAM;
1504 child = tree->cy_child;
1505 while (child != NULL) {
1506 cb = lookup_fn(child->cy_string, table);
1508 snprintf(err_str, sizeof(err_str),
1509 "\"call back for '%s' not found\"",
1511 cYAML_build_error(LUSTRE_CFG_RC_BAD_PARAM, -1,
1512 "yaml", "helper", err_str, err_rc);
1516 if (cYAML_is_sequence(child)) {
1517 while ((head = cYAML_get_next_seq_item(child, &item))
1519 rc = cb(head, show_rc, err_rc);
1520 if (rc != LUSTRE_CFG_RC_NO_ERR)
1524 rc = cb(child, show_rc, err_rc);
1525 if (rc != LUSTRE_CFG_RC_NO_ERR)
1529 child = child->cy_next;
1533 cYAML_free_tree(tree);
1538 int lustre_yaml_config(char *f, struct cYAML **err_rc)
1540 return lustre_yaml_cb_helper(f, lookup_config_tbl,
1544 int lustre_yaml_del(char *f, struct cYAML **err_rc)
1546 return lustre_yaml_cb_helper(f, lookup_del_tbl,
1550 int lustre_yaml_show(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
1552 return lustre_yaml_cb_helper(f, lookup_show_tbl,