📄 stp_cli.c
字号:
/************************************************************************ * RSTP library - Rapid Spanning Tree (802.1t, 802.1w) * Copyright (C) 2001-2003 Optical Access * Author: Alex Rozin * * This file is part of RSTP library. * * RSTP library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by the * Free Software Foundation; version 2.1 * * RSTP library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser * General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with RSTP library; see the file COPYING. If not, write to the Free * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA * 02111-1307, USA. **********************************************************************/#include <stdlib.h>#include <stdio.h>#include <string.h>#include <ctype.h>#include <sys/types.h>#include <netinet/in.h>#include <arpa/inet.h>#include <unistd.h>#include "cli.h"#include "stp_cli.h"#include "bitmap.h"#include "uid_stp.h"#include "stp_in.h"#include "stp_to.h"int I_am_a_stupid_hub = 0;static voidprint_bridge_id (UID_BRIDGE_ID_T *bridge_id, unsigned char cr){ printf("%04lX-%02x%02x%02x%02x%02x%02x", (unsigned long) bridge_id->prio, (unsigned char) bridge_id->addr[0], (unsigned char) bridge_id->addr[1], (unsigned char) bridge_id->addr[2], (unsigned char) bridge_id->addr[3], (unsigned char) bridge_id->addr[4], (unsigned char) bridge_id->addr[5]); if (cr) printf("\n");}static char *stp_state2str (RSTP_PORT_STATE stp_port_state, int detail){ if (detail) { switch (stp_port_state) { case UID_PORT_DISABLED: return "Disabled"; case UID_PORT_DISCARDING: return "Discarding"; case UID_PORT_LEARNING: return "Learning"; case UID_PORT_FORWARDING: return "Forwarding"; case UID_PORT_NON_STP: return "NoStp"; default: return "Unknown"; } } switch (stp_port_state) { case UID_PORT_DISABLED: return "Dis"; case UID_PORT_DISCARDING: return "Blk"; case UID_PORT_LEARNING: return "Lrn"; case UID_PORT_FORWARDING: return "Fwd"; case UID_PORT_NON_STP: return "Non"; default: return "Unk"; }}static void CLI_out_port_id (int port, unsigned char cr){ printf ("%s", STP_OUT_get_port_name (port)); if (cr) printf("\n");}static int cli_enable (int argc, char** argv){ UID_STP_CFG_T uid_cfg; int rc; uid_cfg.field_mask = BR_CFG_STATE; uid_cfg.stp_enabled = STP_ENABLED; rc = STP_IN_stpm_set_cfg (0, NULL, &uid_cfg); if (rc) { printf ("can't enable: %s\n", STP_IN_get_error_explanation (rc)); } else I_am_a_stupid_hub = 0; return 0;}static int cli_disable (int argc, char** argv){ UID_STP_CFG_T uid_cfg; int rc; uid_cfg.field_mask = BR_CFG_STATE; uid_cfg.stp_enabled = STP_DISABLED; rc = STP_IN_stpm_set_cfg (0, NULL, &uid_cfg); if (rc) { printf ("can't disable: %s\n", STP_IN_get_error_explanation (rc)); } else I_am_a_stupid_hub = 1; return 0;}static int cli_br_get_cfg (int argc, char** argv){ UID_STP_STATE_T uid_state; UID_STP_CFG_T uid_cfg; int rc; rc = STP_IN_stpm_get_state (0, &uid_state); if (rc) { printf ("can't get rstp bridge state: %s\n", STP_IN_get_error_explanation (rc)); return 0; } rc = STP_IN_stpm_get_cfg (0, &uid_cfg); if (rc) { printf ("can't get rstp bridge configuration: %s\n", STP_IN_get_error_explanation (rc)); return 0; }#if 0 printf("Interface: %-7s (tag:%d) State: ", uid_state.vlan_name, (int) uid_state.vlan_id);#else printf("Bridge: %-7s State:", uid_state.vlan_name);#endif switch (uid_state.stp_enabled) { case STP_ENABLED: printf("enabled\n"); break; case STP_DISABLED: printf("disabled\n");break; default: printf("unknown\n"); return 0; } printf("BridgeId: "); print_bridge_id (&uid_state.bridge_id, 0); printf(" Bridge Proirity: %lu (0x%lX)\n", (unsigned long) uid_state.bridge_id.prio, (unsigned long) uid_state.bridge_id.prio); if (uid_cfg.force_version < 2) printf("Force Version: stp\n"); printf("Designated Root: "); print_bridge_id (&uid_state.designated_root, 1); if (uid_state.root_port) { printf("Root Port: %04lx (", (unsigned long) uid_state.root_port); CLI_out_port_id (uid_state.root_port & 0xfff, False); printf("), Root Cost: %-lu\n", (unsigned long) uid_state.root_path_cost); } else { printf("Root Port: none\n"); } if (uid_state.Topo_Change) printf ("Topology Change Count: %lu\n", uid_state.Topo_Change_Count); else printf ("Time Since Topology Change: %lu\n", uid_state.timeSince_Topo_Change); printf ("Max Age: %2d Bridge Max Age: %-2d\n", (int) uid_state.max_age, (int) uid_cfg.max_age); printf ("Hello Time: %2d Bridge Hello Time: %-2d\n", (int) uid_state.hello_time, (int) uid_cfg.hello_time); printf ("Forward Delay: %2d Bridge Forward Delay: %-2d\n", (int) uid_state.forward_delay, (int) uid_cfg.forward_delay); printf ("Hold Time: %2d\n", (int) uid_cfg.hold_time); return 0;}static voidshow_rstp_port (BITMAP_T* ports_bitmap, int detail){ UID_STP_STATE_T uid_state; UID_STP_PORT_STATE_T uid_port; UID_STP_PORT_CFG_T uid_cfg; int port_index; int rc; rc = STP_IN_stpm_get_state (0, &uid_state); if (rc) { printf ("can't get rstp bridge state: %s\n", STP_IN_get_error_explanation (rc)); } else if (! detail) { printf (" BridgeId: "); print_bridge_id (&uid_state.bridge_id, 0); printf (" RootId: "); print_bridge_id (&uid_state.designated_root, 1); } for (port_index = 0; port_index <= NUMBER_OF_PORTS; port_index++) { if (! BitmapGetBit(ports_bitmap, port_index - 1)) continue; uid_port.port_no = port_index; rc = STP_IN_port_get_state (0, &uid_port); if (rc) { printf ("can't get rstp port state: %s\n", STP_IN_get_error_explanation (rc)); continue; } memset (&uid_cfg, 0, sizeof (UID_STP_PORT_CFG_T)); rc = STP_IN_port_get_cfg (0, uid_port.port_no, &uid_cfg); if (rc) { printf ("can't get rstp port config: %s\n", STP_IN_get_error_explanation (rc)); continue; } if (detail) { printf("Stp Port "); CLI_out_port_id (port_index, False);#if 0 printf(": PortId: %04lx in vlan '%s' with tag %d:\n", (unsigned long) uid_port.port_id, uid_state.vlan_name, (int) uid_state.vlan_id);#else printf(": PortId: %04lx in Bridge '%s':\n", (unsigned long) uid_port.port_id, uid_state.vlan_name);#endif printf ("Priority: %-d\n", (int) (uid_port.port_id >> 8)); printf ("State: %-16s", stp_state2str (uid_port.state, 1)); printf (" Uptime: %-9lu\n", uid_port.uptime); printf ("PortPathCost: admin: "); if (ADMIN_PORT_PATH_COST_AUTO == uid_cfg.admin_port_path_cost) printf ("%-9s", "Auto"); else printf ("%-9lu", uid_cfg.admin_port_path_cost); printf (" oper: %-9lu\n", uid_port.oper_port_path_cost); printf ("Point2Point: admin: "); switch (uid_cfg.admin_point2point) { case P2P_FORCE_TRUE: printf ("%-9s", "ForceYes"); break; case P2P_FORCE_FALSE: printf ("%-9s", "ForceNo"); break; case P2P_AUTO: printf ("%-9s", "Auto"); break; } printf (" oper: %-9s\n", uid_port.oper_point2point ? "Yes" : "No"); printf ("Edge: admin: %-9s oper: %-9s\n", uid_cfg.admin_edge ? "Y" : "N", uid_port.oper_edge ? "Y" : "N"); printf ("Partner: oper: %-9s\n", uid_port.oper_stp_neigb ? "Slow" : "Rapid"); if (' ' != uid_port.role) { if ('-' != uid_port.role) { printf("PathCost: %-lu\n", (unsigned long) (uid_port.path_cost)); printf("Designated Root: "); print_bridge_id (&uid_port.designated_root, 1); printf("Designated Cost: %-ld\n", (unsigned long) uid_port.designated_cost); printf("Designated Bridge: "); print_bridge_id (&uid_port.designated_bridge, 1); printf("Designated Port: %-4lx\n\r", (unsigned long) uid_port.designated_port); } printf("Role: "); switch (uid_port.role) { case 'A': printf("Alternate\n"); break; case 'B': printf("Backup\n"); break; case 'R': printf("Root\n"); break; case 'D': printf("Designated\n"); break; case '-': printf("NonStp\n"); break; default: printf("Unknown(%c)\n", uid_port.role); break; } if ('R' == uid_port.role || 'D' == uid_port.role) { /* printf("Tc: %c ", uid_port.tc ? 'Y' : 'n'); */ printf("TcAck: %c ", uid_port.top_change_ack ? 'Y' : 'N'); printf("TcWhile: %3d\n", (int) uid_port.tcWhile); } } if (UID_PORT_DISABLED == uid_port.state || '-' == uid_port.role) {#if 0 printf("helloWhen: %3d ", (int) uid_port.helloWhen); printf("lnkWhile: %3d\n", (int) uid_port.lnkWhile); printf("fdWhile: %3d\n", (int) uid_port.fdWhile);#endif } else if ('-' != uid_port.role) { printf("fdWhile: %3d ", (int) uid_port.fdWhile); printf("rcvdInfoWhile: %3d\n", (int) uid_port.rcvdInfoWhile); printf("rbWhile: %3d ", (int) uid_port.rbWhile); printf("rrWhile: %3d\n", (int) uid_port.rrWhile);#if 0 printf("mdelayWhile: %3d ", (int) uid_port.mdelayWhile); printf("lnkWhile: %3d\n", (int) uid_port.lnkWhile); printf("helloWhen: %3d ", (int) uid_port.helloWhen); printf("txCount: %3d\n", (int) uid_port.txCount);#endif } printf("RSTP BPDU rx: %lu\n", (unsigned long) uid_port.rx_rstp_bpdu_cnt); printf("CONFIG BPDU rx: %lu\n", (unsigned long) uid_port.rx_cfg_bpdu_cnt); printf("TCN BPDU rx: %lu\n", (unsigned long) uid_port.rx_tcn_bpdu_cnt); } else { printf("%c%c%c ", (uid_port.oper_point2point) ? ' ' : '*', (uid_port.oper_edge) ? 'E' : ' ', (uid_port.oper_stp_neigb) ? 's' : ' '); CLI_out_port_id (port_index, False); printf(" %04lx %3s ", (unsigned long) uid_port.port_id, stp_state2str (uid_port.state, 0)); printf (" "); print_bridge_id (&uid_port.designated_root, 0); printf(" "); print_bridge_id (&uid_port.designated_bridge, 0); printf(" %4lx %c", (unsigned long) uid_port.designated_port, uid_port.role); printf ("\n"); } }}static int cli_pr_get_cfg (int argc, char** argv){ BITMAP_T ports_bitmap; int port_index; char detail; if ('a' == argv[1][0]) { BitmapSetAllBits(&ports_bitmap); detail = 0; } else { port_index = strtoul(argv[1], 0, 10); BitmapClear(&ports_bitmap); BitmapSetBit(&ports_bitmap, port_index - 1); detail = 1; } show_rstp_port (&ports_bitmap, detail); return 0;}static voidset_bridge_cfg_value (unsigned long value, unsigned long val_mask){ UID_STP_CFG_T uid_cfg; char* val_name; int rc; uid_cfg.field_mask = val_mask; switch (val_mask) { case BR_CFG_STATE: uid_cfg.stp_enabled = value; val_name = "state"; break; case BR_CFG_PRIO: uid_cfg.bridge_priority = value; val_name = "priority"; break; case BR_CFG_AGE: uid_cfg.max_age = value; val_name = "max_age"; break; case BR_CFG_HELLO: uid_cfg.hello_time = value; val_name = "hello_time"; break; case BR_CFG_DELAY: uid_cfg.forward_delay = value; val_name = "forward_delay"; break; case BR_CFG_FORCE_VER: uid_cfg.force_version = value; val_name = "force_version"; break; case BR_CFG_AGE_MODE: case BR_CFG_AGE_TIME: default: printf ("Invalid value mask 0X%lx\n", val_mask); return; break; } rc = STP_IN_stpm_set_cfg (0, NULL, &uid_cfg); if (0 != rc) { printf ("Can't change rstp bridge %s:%s", val_name, STP_IN_get_error_explanation (rc)); } else { printf ("Changed rstp bridge %s\n", val_name); }}static int cli_br_prio (int argc, char** argv){ long br_prio = 32768L; if (strlen (argv[1]) > 2 && (! strncmp (argv[1], "0x", 2) || ! strncmp (argv[1], "0X", 2))) { br_prio = strtoul(argv[1] + 2, 0, 16); } else { br_prio = strtoul(argv[1], 0, 10); } if (! br_prio) { printf ("Warning: newPriority=0, are you sure ?\n"); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -