2 * Copyright (c) 2005-2008 Voltaire, Inc. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 #endif /* HAVE_CONFIG_H */
38 #define _GNU_SOURCE /* for getline */
42 #include <sys/types.h>
43 #include <sys/socket.h>
45 #ifdef ENABLE_OSM_CONSOLE_SOCKET
46 #include <arpa/inet.h>
52 #include <opensm/osm_console.h>
53 #include <complib/cl_passivelock.h>
54 #include <opensm/osm_perfmgr.h>
55 #include <opensm/osm_subnet.h>
59 void (*help_function) (FILE * out, int detail);
60 void (*parse_function) (char **p_last, osm_opensm_t * p_osm,
68 void (*loop_function) (osm_opensm_t * p_osm, FILE * out);
72 .loop_function = NULL,
75 static const struct command console_cmds[];
77 static inline char *next_token(char **p_last)
79 return strtok_r(NULL, " \t\n\r", p_last);
82 static void help_command(FILE * out, int detail)
86 fprintf(out, "Supported commands and syntax:\n");
87 fprintf(out, "help [<command>]\n");
88 /* skip help command */
89 for (i = 1; console_cmds[i].name; i++)
90 console_cmds[i].help_function(out, 0);
93 static void help_quit(FILE * out, int detail)
95 fprintf(out, "quit (not valid in local mode; use ctl-c)\n");
98 static void help_loglevel(FILE * out, int detail)
100 fprintf(out, "loglevel [<log-level>]\n");
102 fprintf(out, " log-level is OR'ed from the following\n");
103 fprintf(out, " OSM_LOG_NONE 0x%02X\n",
105 fprintf(out, " OSM_LOG_ERROR 0x%02X\n",
107 fprintf(out, " OSM_LOG_INFO 0x%02X\n",
109 fprintf(out, " OSM_LOG_VERBOSE 0x%02X\n",
111 fprintf(out, " OSM_LOG_DEBUG 0x%02X\n",
113 fprintf(out, " OSM_LOG_FUNCS 0x%02X\n",
115 fprintf(out, " OSM_LOG_FRAMES 0x%02X\n",
117 fprintf(out, " OSM_LOG_ROUTING 0x%02X\n",
119 fprintf(out, " OSM_LOG_SYS 0x%02X\n",
122 fprintf(out, " OSM_LOG_DEFAULT_LEVEL 0x%02X\n",
123 OSM_LOG_DEFAULT_LEVEL);
127 static void help_priority(FILE * out, int detail)
129 fprintf(out, "priority [<sm-priority>]\n");
132 static void help_resweep(FILE * out, int detail)
134 fprintf(out, "resweep [heavy|light]\n");
137 static void help_reroute(FILE * out, int detail)
139 fprintf(out, "reroute\n");
141 fprintf(out, "reroute the fabric\n");
145 static void help_status(FILE * out, int detail)
147 fprintf(out, "status [loop]\n");
149 fprintf(out, " loop -- type \"q<ret>\" to quit\n");
153 static void help_logflush(FILE * out, int detail)
155 fprintf(out, "logflush -- flush the opensm.log file\n");
158 static void help_querylid(FILE * out, int detail)
161 "querylid lid -- print internal information about the lid specified\n");
164 static void help_portstatus(FILE * out, int detail)
166 fprintf(out, "portstatus [ca|switch|router]\n");
168 fprintf(out, "summarize port status\n");
170 " [ca|switch|router] -- limit the results to the node type specified\n");
175 static void help_switchbalance(FILE * out, int detail)
177 fprintf(out, "switchbalance [verbose] [guid]\n");
179 fprintf(out, "output switch balancing information\n");
181 " [verbose] -- verbose output\n"
182 " [guid] -- limit results to specified guid\n");
186 static void help_lidbalance(FILE * out, int detail)
188 fprintf(out, "lidbalance [switchguid]\n");
190 fprintf(out, "output lid balanced forwarding information\n");
192 " [switchguid] -- limit results to specified switch guid\n");
196 static void help_dump_conf(FILE *out, int detail)
198 fprintf(out, "dump_conf\n");
200 fprintf(out, "dump current opensm configuration\n");
204 #ifdef ENABLE_OSM_PERF_MGR
205 static void help_perfmgr(FILE * out, int detail)
208 "perfmgr [enable|disable|clear_counters|dump_counters|sweep_time[seconds]]\n");
211 "perfmgr -- print the performance manager state\n");
213 " [enable|disable] -- change the perfmgr state\n");
215 " [sweep_time] -- change the perfmgr sweep time (requires [seconds] option)\n");
217 " [clear_counters] -- clear the counters stored\n");
219 " [dump_counters [mach]] -- dump the counters (optionally in [mach]ine readable format)\n");
221 " [print_counters <nodename|nodeguid>] -- print the counters for the specified node\n");
224 #endif /* ENABLE_OSM_PERF_MGR */
226 /* more help routines go here */
228 static void help_parse(char **p_last, osm_opensm_t * p_osm, FILE * out)
233 p_cmd = next_token(p_last);
235 help_command(out, 0);
237 for (i = 1; console_cmds[i].name; i++) {
238 if (!strcmp(p_cmd, console_cmds[i].name)) {
240 console_cmds[i].help_function(out, 1);
245 fprintf(out, "%s : Command not found\n\n", p_cmd);
246 help_command(out, 0);
251 static void loglevel_parse(char **p_last, osm_opensm_t * p_osm, FILE * out)
256 p_cmd = next_token(p_last);
258 fprintf(out, "Current log level is 0x%x\n",
259 osm_log_get_level(&p_osm->log));
261 /* Handle x, 0x, and decimal specification of log level */
262 if (!strncmp(p_cmd, "x", 1)) {
264 level = strtoul(p_cmd, NULL, 16);
266 if (!strncmp(p_cmd, "0x", 2)) {
268 level = strtoul(p_cmd, NULL, 16);
270 level = strtol(p_cmd, NULL, 10);
272 if ((level >= 0) && (level < 256)) {
273 fprintf(out, "Setting log level to 0x%x\n", level);
274 osm_log_set_level(&p_osm->log, level);
276 fprintf(out, "Invalid log level 0x%x\n", level);
280 static void priority_parse(char **p_last, osm_opensm_t * p_osm, FILE * out)
285 p_cmd = next_token(p_last);
287 fprintf(out, "Current sm-priority is %d\n",
288 p_osm->subn.opt.sm_priority);
290 priority = strtol(p_cmd, NULL, 0);
291 if (0 > priority || 15 < priority)
293 "Invalid sm-priority %d; must be between 0 and 15\n",
296 fprintf(out, "Setting sm-priority to %d\n", priority);
297 osm_set_sm_priority(&p_osm->sm, (uint8_t)priority);
302 static char *sm_state_str(int state)
305 case IB_SMINFO_STATE_DISCOVERING:
306 return ("Discovering");
307 case IB_SMINFO_STATE_STANDBY:
309 case IB_SMINFO_STATE_NOTACTIVE:
310 return ("Not Active");
311 case IB_SMINFO_STATE_MASTER:
317 static char *sa_state_str(osm_sa_state_t state)
320 case OSM_SA_STATE_INIT:
322 case OSM_SA_STATE_READY:
328 static void print_status(osm_opensm_t * p_osm, FILE * out)
330 cl_list_item_t *item;
333 cl_plock_acquire(&p_osm->lock);
334 fprintf(out, " OpenSM Version : %s\n", p_osm->osm_version);
335 fprintf(out, " SM State : %s\n",
336 sm_state_str(p_osm->subn.sm_state));
337 fprintf(out, " SA State : %s\n",
338 sa_state_str(p_osm->sa.state));
339 fprintf(out, " Routing Engine : %s\n",
340 osm_routing_engine_type_str(p_osm->
341 routing_engine_used));
343 fprintf(out, " Loaded event plugins :");
344 if (cl_qlist_head(&p_osm->plugin_list) ==
345 cl_qlist_end(&p_osm->plugin_list)) {
346 fprintf(out, " <none>");
348 for (item = cl_qlist_head(&p_osm->plugin_list);
349 item != cl_qlist_end(&p_osm->plugin_list);
350 item = cl_qlist_next(item))
352 ((osm_epi_plugin_t *)item)->plugin_name);
355 #ifdef ENABLE_OSM_PERF_MGR
356 fprintf(out, "\n PerfMgr state/sweep state : %s/%s\n",
357 osm_perfmgr_get_state_str(&(p_osm->perfmgr)),
358 osm_perfmgr_get_sweep_state_str(&(p_osm->perfmgr)));
360 fprintf(out, "\n MAD stats\n"
362 " QP0 MADs outstanding : %d\n"
363 " QP0 MADs outstanding (on wire) : %d\n"
364 " QP0 MADs rcvd : %d\n"
365 " QP0 MADs sent : %d\n"
366 " QP0 unicasts sent : %d\n"
367 " QP0 unknown MADs rcvd : %d\n"
368 " SA MADs outstanding : %d\n"
369 " SA MADs rcvd : %d\n"
370 " SA MADs sent : %d\n"
371 " SA unknown MADs rcvd : %d\n"
372 " SA MADs ignored : %d\n",
373 p_osm->stats.qp0_mads_outstanding,
374 p_osm->stats.qp0_mads_outstanding_on_wire,
375 p_osm->stats.qp0_mads_rcvd,
376 p_osm->stats.qp0_mads_sent,
377 p_osm->stats.qp0_unicasts_sent,
378 p_osm->stats.qp0_mads_rcvd_unknown,
379 p_osm->stats.sa_mads_outstanding,
380 p_osm->stats.sa_mads_rcvd,
381 p_osm->stats.sa_mads_sent,
382 p_osm->stats.sa_mads_rcvd_unknown,
383 p_osm->stats.sa_mads_ignored);
384 fprintf(out, "\n Subnet flags\n"
386 " Ignore existing lfts : %d\n"
387 " Subnet Init errors : %d\n"
388 " In sweep hop 0 : %d\n"
389 " First time master sweep : %d\n"
390 " Coming out of standby : %d\n",
391 p_osm->subn.ignore_existing_lfts,
392 p_osm->subn.subnet_initialization_error,
393 p_osm->subn.in_sweep_hop_0,
394 p_osm->subn.first_time_master_sweep,
395 p_osm->subn.coming_out_of_standby);
397 cl_plock_release(&p_osm->lock);
401 static int loop_command_check_time(void)
403 time_t cur = time(NULL);
404 if ((loop_command.previous + loop_command.delay_s) < cur) {
405 loop_command.previous = cur;
411 static void status_parse(char **p_last, osm_opensm_t * p_osm, FILE * out)
415 p_cmd = next_token(p_last);
417 if (strcmp(p_cmd, "loop") == 0) {
418 fprintf(out, "Looping on status command...\n");
421 loop_command.previous = time(NULL);
422 loop_command.loop_function = print_status;
428 print_status(p_osm, out);
431 static void resweep_parse(char **p_last, osm_opensm_t * p_osm, FILE * out)
435 p_cmd = next_token(p_last);
437 (strcmp(p_cmd, "heavy") != 0 && strcmp(p_cmd, "light") != 0)) {
438 fprintf(out, "Invalid resweep command\n");
439 help_resweep(out, 1);
441 if (strcmp(p_cmd, "heavy") == 0)
442 p_osm->subn.force_heavy_sweep = TRUE;
443 osm_opensm_sweep(p_osm);
447 static void reroute_parse(char **p_last, osm_opensm_t * p_osm, FILE * out)
449 p_osm->subn.force_reroute = TRUE;
450 osm_opensm_sweep(p_osm);
453 static void logflush_parse(char **p_last, osm_opensm_t * p_osm, FILE * out)
455 fflush(p_osm->log.out_port);
458 static void querylid_parse(char **p_last, osm_opensm_t * p_osm, FILE * out)
462 osm_port_t *p_port = NULL;
463 char *p_cmd = next_token(p_last);
466 fprintf(out, "no LID specified\n");
467 help_querylid(out, 1);
471 lid = (uint16_t) strtoul(p_cmd, NULL, 0);
472 cl_plock_acquire(&p_osm->lock);
473 if (lid > cl_ptr_vector_get_capacity(&(p_osm->subn.port_lid_tbl)))
475 p_port = cl_ptr_vector_get(&(p_osm->subn.port_lid_tbl), lid);
479 fprintf(out, "Query results for LID %u\n", lid);
481 " GUID : 0x%016" PRIx64 "\n"
485 cl_ntoh64(p_port->guid),
486 p_port->p_node->print_desc,
487 ib_get_node_type_str(osm_node_get_type(p_port->p_node)),
488 p_port->p_node->node_info.num_ports);
490 if (p_port->p_node->sw)
494 for ( /* see above */ ; p < p_port->p_node->physp_tbl_size; p++) {
496 " Port %d health : %s\n",
498 p_port->p_node->physp_table[p].
499 healthy ? "OK" : "ERROR");
502 cl_plock_release(&p_osm->lock);
506 cl_plock_release(&p_osm->lock);
507 fprintf(out, "Invalid lid %d\n", lid);
512 * Data structures for the portstatus command
514 typedef struct _port_report {
515 struct _port_report *next;
518 char print_desc[IB_NODE_DESCRIPTION_SIZE + 1];
522 __tag_port_report(port_report_t ** head, uint64_t node_guid,
523 uint8_t port_num, char *print_desc)
525 port_report_t *rep = malloc(sizeof(*rep));
529 rep->node_guid = node_guid;
530 rep->port_num = port_num;
531 memcpy(rep->print_desc, print_desc, IB_NODE_DESCRIPTION_SIZE + 1);
540 static void __print_port_report(FILE * out, port_report_t * head)
542 port_report_t *item = head;
543 while (item != NULL) {
544 fprintf(out, " 0x%016" PRIx64 " %d (%s)\n",
545 item->node_guid, item->port_num, item->print_desc);
546 port_report_t *next = item->next;
553 uint8_t node_type_lim; /* limit the results; 0 == ALL */
554 uint64_t total_nodes;
555 uint64_t total_ports;
557 uint64_t ports_active;
558 uint64_t ports_disabled;
559 port_report_t *disabled_ports;
564 uint64_t ports_unknown_width;
565 uint64_t ports_reduced_width;
566 port_report_t *reduced_width_ports;
570 uint64_t ports_unknown_speed;
571 uint64_t ports_reduced_speed;
572 port_report_t *reduced_speed_ports;
576 * iterator function to get portstatus on each node
578 static void __get_stats(cl_map_item_t * const p_map_item, void *context)
580 fabric_stats_t *fs = (fabric_stats_t *) context;
581 osm_node_t *node = (osm_node_t *) p_map_item;
582 uint8_t num_ports = osm_node_get_num_physp(node);
585 /* Skip nodes we are not interested in */
586 if (fs->node_type_lim != 0
587 && fs->node_type_lim != node->node_info.node_type)
592 for (port = 1; port < num_ports; port++) {
593 osm_physp_t *phys = osm_node_get_physp_ptr(node, port);
594 ib_port_info_t *pi = NULL;
595 uint8_t active_speed = 0;
596 uint8_t enabled_speed = 0;
597 uint8_t active_width = 0;
598 uint8_t enabled_width = 0;
599 uint8_t port_state = 0;
600 uint8_t port_phys_state = 0;
605 pi = &(phys->port_info);
606 active_speed = ib_port_info_get_link_speed_active(pi);
607 enabled_speed = ib_port_info_get_link_speed_enabled(pi);
608 active_width = pi->link_width_active;
609 enabled_width = pi->link_width_enabled;
610 port_state = ib_port_info_get_port_state(pi);
611 port_phys_state = ib_port_info_get_port_phys_state(pi);
613 if ((enabled_width ^ active_width) > active_width) {
614 __tag_port_report(&(fs->reduced_width_ports),
615 cl_ntoh64(node->node_info.node_guid),
616 port, node->print_desc);
617 fs->ports_reduced_width++;
620 if ((enabled_speed ^ active_speed) > active_speed) {
621 __tag_port_report(&(fs->reduced_speed_ports),
622 cl_ntoh64(node->node_info.node_guid),
623 port, node->print_desc);
624 fs->ports_reduced_speed++;
627 switch (active_speed) {
628 case IB_LINK_SPEED_ACTIVE_2_5:
631 case IB_LINK_SPEED_ACTIVE_5:
634 case IB_LINK_SPEED_ACTIVE_10:
638 fs->ports_unknown_speed++;
641 switch (active_width) {
642 case IB_LINK_WIDTH_ACTIVE_1X:
645 case IB_LINK_WIDTH_ACTIVE_4X:
648 case IB_LINK_WIDTH_ACTIVE_8X:
651 case IB_LINK_WIDTH_ACTIVE_12X:
655 fs->ports_unknown_width++;
658 if (port_state == IB_LINK_DOWN)
660 else if (port_state == IB_LINK_ACTIVE)
662 if (port_phys_state == IB_PORT_PHYS_STATE_DISABLED) {
663 __tag_port_report(&(fs->disabled_ports),
664 cl_ntoh64(node->node_info.node_guid),
665 port, node->print_desc);
666 fs->ports_disabled++;
673 static void portstatus_parse(char **p_last, osm_opensm_t * p_osm, FILE * out)
676 struct timeval before, after;
679 memset(&fs, 0, sizeof(fs));
681 p_cmd = next_token(p_last);
683 if (strcmp(p_cmd, "ca") == 0) {
684 fs.node_type_lim = IB_NODE_TYPE_CA;
685 } else if (strcmp(p_cmd, "switch") == 0) {
686 fs.node_type_lim = IB_NODE_TYPE_SWITCH;
687 } else if (strcmp(p_cmd, "router") == 0) {
688 fs.node_type_lim = IB_NODE_TYPE_ROUTER;
690 fprintf(out, "Node type not understood\n");
691 help_portstatus(out, 1);
696 gettimeofday(&before, NULL);
698 /* for each node in the system gather the stats */
699 cl_plock_acquire(&p_osm->lock);
700 cl_qmap_apply_func(&(p_osm->subn.node_guid_tbl), __get_stats,
702 cl_plock_release(&p_osm->lock);
704 gettimeofday(&after, NULL);
706 /* report the stats */
707 fprintf(out, "\"%s\" port status:\n",
708 fs.node_type_lim ? ib_get_node_type_str(fs.
709 node_type_lim) : "ALL");
711 " %" PRIu64 " port(s) scanned on %" PRIu64
712 " nodes in %lu us\n", fs.total_ports, fs.total_nodes,
713 after.tv_usec - before.tv_usec);
716 fprintf(out, " %" PRIu64 " down\n", fs.ports_down);
718 fprintf(out, " %" PRIu64 " active\n", fs.ports_active);
720 fprintf(out, " %" PRIu64 " at 1X\n", fs.ports_1X);
722 fprintf(out, " %" PRIu64 " at 4X\n", fs.ports_4X);
724 fprintf(out, " %" PRIu64 " at 8X\n", fs.ports_8X);
726 fprintf(out, " %" PRIu64 " at 12X\n", fs.ports_12X);
729 fprintf(out, " %" PRIu64 " at 2.5 Gbps\n", fs.ports_sdr);
731 fprintf(out, " %" PRIu64 " at 5.0 Gbps\n", fs.ports_ddr);
733 fprintf(out, " %" PRIu64 " at 10.0 Gbps\n", fs.ports_qdr);
735 if (fs.ports_disabled + fs.ports_reduced_speed + fs.ports_reduced_width
737 fprintf(out, "\nPossible issues:\n");
739 if (fs.ports_disabled) {
740 fprintf(out, " %" PRIu64 " disabled\n", fs.ports_disabled);
741 __print_port_report(out, fs.disabled_ports);
743 if (fs.ports_reduced_speed) {
744 fprintf(out, " %" PRIu64 " with reduced speed\n",
745 fs.ports_reduced_speed);
746 __print_port_report(out, fs.reduced_speed_ports);
748 if (fs.ports_reduced_width) {
749 fprintf(out, " %" PRIu64 " with reduced width\n",
750 fs.ports_reduced_width);
751 __print_port_report(out, fs.reduced_width_ports);
756 static void switchbalance_check(osm_opensm_t * p_osm,
757 osm_switch_t * p_sw, FILE * out, int verbose)
761 const cl_qmap_t *p_port_tbl;
763 osm_physp_t *p_physp;
764 osm_physp_t *p_rem_physp;
765 osm_node_t *p_rem_node;
766 uint32_t count[255]; /* max ports is a uint8_t */
767 uint8_t output_ports[255];
768 uint8_t output_ports_count = 0;
769 uint32_t min_count = 0xFFFFFFFF;
770 uint32_t max_count = 0;
773 memset(count, '\0', sizeof(uint32_t) * 255);
775 /* Count port usage */
776 p_port_tbl = &p_osm->subn.port_guid_tbl;
777 for (p_port = (osm_port_t *) cl_qmap_head(p_port_tbl);
778 p_port != (osm_port_t *) cl_qmap_end(p_port_tbl);
779 p_port = (osm_port_t *) cl_qmap_next(&p_port->map_item)) {
784 /* Don't count switches in port usage */
785 if (osm_node_get_type(p_port->p_node) == IB_NODE_TYPE_SWITCH)
788 osm_port_get_lid_range_ho(p_port, &min_lid_ho, &max_lid_ho);
790 if (min_lid_ho == 0 || max_lid_ho == 0)
793 for (lid_ho = min_lid_ho; lid_ho <= max_lid_ho; lid_ho++) {
794 port_num = osm_switch_get_port_by_lid(p_sw, lid_ho);
795 if (port_num == OSM_NO_PATH)
802 num_ports = p_sw->num_ports;
803 for (port_num = 1; port_num < num_ports; port_num++) {
804 p_physp = osm_node_get_physp_ptr(p_sw->p_node, port_num);
806 /* if port is down/unhealthy, don't consider it in
807 * min/max calculations
809 if (!p_physp || !osm_physp_is_healthy(p_physp)
810 || !osm_physp_get_remote(p_physp))
813 p_rem_physp = osm_physp_get_remote(p_physp);
814 p_rem_node = osm_physp_get_node_ptr(p_rem_physp);
816 /* If we are directly connected to a CA/router, its not really
817 * up for balancing consideration.
819 if (osm_node_get_type(p_rem_node) != IB_NODE_TYPE_SWITCH)
822 output_ports[output_ports_count] = port_num;
823 output_ports_count++;
825 if (count[port_num] < min_count)
826 min_count = count[port_num];
827 if (count[port_num] > max_count)
828 max_count = count[port_num];
831 if (verbose || ((max_count - min_count) > 1)) {
832 if ((max_count - min_count) > 1)
834 "Unbalanced Switch: 0x%016" PRIx64 " (%s)\n",
835 cl_ntoh64(p_sw->p_node->node_info.node_guid),
836 p_sw->p_node->print_desc);
839 "Switch: 0x%016" PRIx64 " (%s)\n",
840 cl_ntoh64(p_sw->p_node->node_info.node_guid),
841 p_sw->p_node->print_desc);
843 for (i = 0; i < output_ports_count; i++) {
846 output_ports[i], count[output_ports[i]]);
851 static void switchbalance_parse(char **p_last, osm_opensm_t * p_osm, FILE * out)
858 p_cmd = next_token(p_last);
862 if (strcmp(p_cmd, "verbose") == 0) {
864 p_cmd = next_token(p_last);
868 guid = strtoull(p_cmd, &p_end, 0);
869 if (!guid || *p_end != '\0') {
870 fprintf(out, "Invalid guid specified\n");
871 help_switchbalance(out, 1);
877 cl_plock_acquire(&p_osm->lock);
879 p_sw = osm_get_switch_by_guid(&p_osm->subn, cl_hton64(guid));
881 fprintf(out, "guid not found\n");
885 switchbalance_check(p_osm, p_sw, out, verbose);
887 cl_qmap_t *p_sw_guid_tbl = &p_osm->subn.sw_guid_tbl;
888 for (p_sw = (osm_switch_t *) cl_qmap_head(p_sw_guid_tbl);
889 p_sw != (osm_switch_t *) cl_qmap_end(p_sw_guid_tbl);
890 p_sw = (osm_switch_t *) cl_qmap_next(&p_sw->map_item))
891 switchbalance_check(p_osm, p_sw, out, verbose);
894 cl_plock_release(&p_osm->lock);
898 static void lidbalance_check(osm_opensm_t * p_osm,
899 osm_switch_t * p_sw, FILE * out)
902 const cl_qmap_t *p_port_tbl;
905 p_port_tbl = &p_osm->subn.port_guid_tbl;
906 for (p_port = (osm_port_t *) cl_qmap_head(p_port_tbl);
907 p_port != (osm_port_t *) cl_qmap_end(p_port_tbl);
908 p_port = (osm_port_t *) cl_qmap_next(&p_port->map_item)) {
909 uint32_t port_count[255]; /* max ports is a uint8_t */
910 osm_node_t *rem_node[255];
911 uint32_t rem_node_count;
912 uint32_t rem_count[255];
913 osm_physp_t *p_physp;
914 osm_physp_t *p_rem_physp;
915 osm_node_t *p_rem_node;
916 uint32_t port_min_count = 0xFFFFFFFF;
917 uint32_t port_max_count = 0;
918 uint32_t rem_min_count = 0xFFFFFFFF;
919 uint32_t rem_max_count = 0;
926 /* we only care about non-switches */
927 if (osm_node_get_type(p_port->p_node) == IB_NODE_TYPE_SWITCH)
930 osm_port_get_lid_range_ho(p_port, &min_lid_ho, &max_lid_ho);
932 if (min_lid_ho == 0 || max_lid_ho == 0)
935 memset(port_count, '\0', sizeof(uint32_t) * 255);
936 memset(rem_node, '\0', sizeof(osm_node_t *) * 255);
938 memset(rem_count, '\0', sizeof(uint32_t) * 255);
940 for (lid_ho = min_lid_ho; lid_ho <= max_lid_ho; lid_ho++) {
941 boolean_t rem_node_found = FALSE;
942 unsigned int indx = 0;
944 port_num = osm_switch_get_port_by_lid(p_sw, lid_ho);
945 if (port_num == OSM_NO_PATH)
949 osm_node_get_physp_ptr(p_sw->p_node, port_num);
951 /* if port is down/unhealthy, can't calculate */
952 if (!p_physp || !osm_physp_is_healthy(p_physp)
953 || !osm_physp_get_remote(p_physp))
956 p_rem_physp = osm_physp_get_remote(p_physp);
957 p_rem_node = osm_physp_get_node_ptr(p_rem_physp);
959 /* determine if we've seen this remote node before.
960 * If not, store it. If yes, update the counter
962 for (i = 0; i < rem_node_count; i++) {
963 if (rem_node[i] == p_rem_node) {
964 rem_node_found = TRUE;
970 if (!rem_node_found) {
971 rem_node[rem_node_count] = p_rem_node;
972 rem_count[rem_node_count]++;
973 indx = rem_node_count;
978 port_count[port_num]++;
984 for (i = 0; i < rem_node_count; i++) {
985 if (rem_count[i] < rem_min_count)
986 rem_min_count = rem_count[i];
987 if (rem_count[i] > rem_max_count)
988 rem_max_count = rem_count[i];
991 num_ports = p_sw->num_ports;
992 for (i = 0; i < num_ports; i++) {
995 if (port_count[i] < port_min_count)
996 port_min_count = port_count[i];
997 if (port_count[i] > port_max_count)
998 port_max_count = port_count[i];
1001 /* Output if this CA/router is being forwarded an unbalanced number of
1002 * times to a destination.
1004 if ((rem_max_count - rem_min_count) > 1) {
1006 "Unbalanced Remote Forwarding: Switch 0x%016"
1008 cl_ntoh64(p_sw->p_node->node_info.node_guid),
1009 p_sw->p_node->print_desc);
1010 if (osm_node_get_type(p_port->p_node) ==
1013 else if (osm_node_get_type(p_port->p_node) ==
1014 IB_NODE_TYPE_ROUTER)
1015 fprintf(out, "Router");
1016 fprintf(out, " 0x%016" PRIx64 " (%s): ",
1017 cl_ntoh64(p_port->p_node->node_info.node_guid),
1018 p_port->p_node->print_desc);
1019 for (i = 0; i < rem_node_count; i++) {
1021 "Dest 0x%016" PRIx64 "(%s) - %u ",
1022 cl_ntoh64(rem_node[i]->node_info.
1024 rem_node[i]->print_desc, rem_count[i]);
1029 /* Output if this CA/router is being forwarded through a port
1030 * an unbalanced number of times.
1032 if ((port_max_count - port_min_count) > 1) {
1034 "Unbalanced Port Forwarding: Switch 0x%016"
1036 cl_ntoh64(p_sw->p_node->node_info.node_guid),
1037 p_sw->p_node->print_desc);
1038 if (osm_node_get_type(p_port->p_node) ==
1041 else if (osm_node_get_type(p_port->p_node) ==
1042 IB_NODE_TYPE_ROUTER)
1043 fprintf(out, "Router");
1044 fprintf(out, " 0x%016" PRIx64 " (%s): ",
1045 cl_ntoh64(p_port->p_node->node_info.node_guid),
1046 p_port->p_node->print_desc);
1047 for (i = 0; i < num_ports; i++) {
1050 fprintf(out, "Port %u - %u: ", i,
1058 static void lidbalance_parse(char **p_last, osm_opensm_t * p_osm, FILE * out)
1064 p_cmd = next_token(p_last);
1068 guid = strtoull(p_cmd, &p_end, 0);
1069 if (!guid || *p_end != '\0') {
1070 fprintf(out, "Invalid switchguid specified\n");
1071 help_lidbalance(out, 1);
1076 cl_plock_acquire(&p_osm->lock);
1078 p_sw = osm_get_switch_by_guid(&p_osm->subn, cl_hton64(guid));
1080 fprintf(out, "switchguid not found\n");
1083 lidbalance_check(p_osm, p_sw, out);
1085 cl_qmap_t *p_sw_guid_tbl = &p_osm->subn.sw_guid_tbl;
1086 for (p_sw = (osm_switch_t *) cl_qmap_head(p_sw_guid_tbl);
1087 p_sw != (osm_switch_t *) cl_qmap_end(p_sw_guid_tbl);
1088 p_sw = (osm_switch_t *) cl_qmap_next(&p_sw->map_item))
1089 lidbalance_check(p_osm, p_sw, out);
1093 cl_plock_release(&p_osm->lock);
1097 static void dump_conf_parse(char **p_last, osm_opensm_t * p_osm, FILE * out)
1099 osm_subn_output_conf(out, &p_osm->subn.opt);
1102 #ifdef ENABLE_OSM_PERF_MGR
1103 static void perfmgr_parse(char **p_last, osm_opensm_t * p_osm, FILE * out)
1107 p_cmd = next_token(p_last);
1109 if (strcmp(p_cmd, "enable") == 0) {
1110 osm_perfmgr_set_state(&(p_osm->perfmgr),
1111 PERFMGR_STATE_ENABLED);
1112 } else if (strcmp(p_cmd, "disable") == 0) {
1113 osm_perfmgr_set_state(&(p_osm->perfmgr),
1114 PERFMGR_STATE_DISABLE);
1115 } else if (strcmp(p_cmd, "clear_counters") == 0) {
1116 osm_perfmgr_clear_counters(&(p_osm->perfmgr));
1117 } else if (strcmp(p_cmd, "dump_counters") == 0) {
1118 p_cmd = next_token(p_last);
1119 if (p_cmd && (strcmp(p_cmd, "mach") == 0)) {
1120 osm_perfmgr_dump_counters(&(p_osm->perfmgr),
1121 PERFMGR_EVENT_DB_DUMP_MR);
1123 osm_perfmgr_dump_counters(&(p_osm->perfmgr),
1124 PERFMGR_EVENT_DB_DUMP_HR);
1126 } else if (strcmp(p_cmd, "print_counters") == 0) {
1127 p_cmd = next_token(p_last);
1129 osm_perfmgr_print_counters(&(p_osm->perfmgr),
1133 "print_counters requires a node name to be specified\n");
1135 } else if (strcmp(p_cmd, "sweep_time") == 0) {
1136 p_cmd = next_token(p_last);
1138 uint16_t time_s = atoi(p_cmd);
1139 osm_perfmgr_set_sweep_time_s(&(p_osm->perfmgr),
1143 "sweep_time requires a time period (in seconds) to be specified\n");
1146 fprintf(out, "\"%s\" option not found\n", p_cmd);
1149 fprintf(out, "Performance Manager status:\n"
1151 "sweep state : %s\n"
1152 "sweep time : %us\n"
1153 "outstanding queries/max : %d/%u\n",
1154 osm_perfmgr_get_state_str(&(p_osm->perfmgr)),
1155 osm_perfmgr_get_sweep_state_str(&(p_osm->perfmgr)),
1156 osm_perfmgr_get_sweep_time_s(&(p_osm->perfmgr)),
1157 p_osm->perfmgr.outstanding_queries,
1158 p_osm->perfmgr.max_outstanding_queries);
1161 #endif /* ENABLE_OSM_PERF_MGR */
1163 static void quit_parse(char **p_last, osm_opensm_t * p_osm, FILE * out)
1165 osm_console_exit(&p_osm->console, &p_osm->log);
1168 static void help_version(FILE * out, int detail)
1170 fprintf(out, "version -- print the OSM version\n");
1173 static void version_parse(char **p_last, osm_opensm_t * p_osm, FILE * out)
1175 fprintf(out, "%s build %s %s\n", p_osm->osm_version, __DATE__, __TIME__);
1178 /* more parse routines go here */
1180 static const struct command console_cmds[] = {
1181 {"help", &help_command, &help_parse},
1182 {"quit", &help_quit, &quit_parse},
1183 {"loglevel", &help_loglevel, &loglevel_parse},
1184 {"priority", &help_priority, &priority_parse},
1185 {"resweep", &help_resweep, &resweep_parse},
1186 {"reroute", &help_reroute, &reroute_parse},
1187 {"status", &help_status, &status_parse},
1188 {"logflush", &help_logflush, &logflush_parse},
1189 {"querylid", &help_querylid, &querylid_parse},
1190 {"portstatus", &help_portstatus, &portstatus_parse},
1191 {"switchbalance", &help_switchbalance, &switchbalance_parse},
1192 {"lidbalance", &help_lidbalance, &lidbalance_parse},
1193 {"dump_conf", &help_dump_conf, &dump_conf_parse},
1194 {"version", &help_version, &version_parse},
1195 #ifdef ENABLE_OSM_PERF_MGR
1196 {"perfmgr", &help_perfmgr, &perfmgr_parse},
1197 #endif /* ENABLE_OSM_PERF_MGR */
1198 {NULL, NULL, NULL} /* end of array */
1201 static void parse_cmd_line(char *line, osm_opensm_t * p_osm)
1203 char *p_cmd, *p_last;
1205 FILE *out = p_osm->console.out;
1207 while (isspace(*line))
1212 /* find first token which is the command */
1213 p_cmd = strtok_r(line, " \t\n\r", &p_last);
1215 for (i = 0; console_cmds[i].name; i++) {
1216 if (loop_command.on) {
1217 if (!strcmp(p_cmd, "q")) {
1218 loop_command.on = 0;
1223 if (!strcmp(p_cmd, console_cmds[i].name)) {
1225 console_cmds[i].parse_function(&p_last, p_osm,
1231 fprintf(out, "%s : Command not found\n\n", p_cmd);
1232 help_command(out, 0);
1235 fprintf(out, "Error parsing command line: `%s'\n", line);
1237 if (loop_command.on) {
1238 fprintf(out, "use \"q<ret>\" to quit loop\n");
1243 void osm_console(osm_opensm_t * p_osm)
1245 struct pollfd pollfd[2];
1251 osm_console_t *p_oct = &p_osm->console;
1252 osm_log_t *p_log = &p_osm->log;
1254 pollfd[0].fd = p_oct->socket;
1255 pollfd[0].events = POLLIN;
1256 pollfd[0].revents = 0;
1258 pollfd[1].fd = p_oct->in_fd;
1259 pollfd[1].events = POLLIN;
1260 pollfd[1].revents = 0;
1262 fds = p_oct->socket < 0 ? &pollfd[1] : pollfd;
1263 nfds = p_oct->socket < 0 || pollfd[1].fd < 0 ? 1 : 2;
1265 if (loop_command.on && loop_command_check_time() &&
1266 loop_command.loop_function) {
1268 loop_command.loop_function(p_osm, p_oct->out);
1271 loop_command.on = 0;
1275 if (poll(fds, nfds, 1000) <= 0)
1278 #ifdef ENABLE_OSM_CONSOLE_SOCKET
1279 if (pollfd[0].revents & POLLIN) {
1281 struct sockaddr_in sin;
1282 socklen_t len = sizeof(sin);
1283 struct hostent *hent;
1284 if ((new_fd = accept(p_oct->socket, &sin, &len)) < 0) {
1285 OSM_LOG(p_log, OSM_LOG_ERROR,
1286 "ERR 4B04: Failed to accept console socket: %s\n",
1292 (AF_INET, &sin.sin_addr, p_oct->client_ip,
1293 sizeof(p_oct->client_ip)) == NULL) {
1294 snprintf(p_oct->client_ip, 64, "STRING_UNKNOWN");
1296 if ((hent = gethostbyaddr((const char *)&sin.sin_addr,
1297 sizeof(struct in_addr),
1298 AF_INET)) == NULL) {
1299 snprintf(p_oct->client_hn, 128, "STRING_UNKNOWN");
1301 snprintf(p_oct->client_hn, 128, "%s", hent->h_name);
1303 if (is_authorized(p_oct)) {
1304 cio_open(p_oct, new_fd, p_log);
1306 OSM_LOG(p_log, OSM_LOG_ERROR,
1307 "ERR 4B05: Console connection denied: %s (%s)\n",
1308 p_oct->client_hn, p_oct->client_ip);
1315 if (pollfd[1].revents & POLLIN) {
1317 /* Get input line */
1318 n = getline(&p_line, &len, p_oct->in);
1320 /* Parse and act on input */
1321 parse_cmd_line(p_line, p_osm);
1322 if (!loop_command.on) {
1323 osm_console_prompt(p_oct->out);
1326 osm_console_exit(p_oct, p_log);