您的位置:首页 > 其它

快速生成树之端口状态转移状态机

2010-04-01 11:00 549 查看
1 源码

rstplib.1.1.02/sttrans.c,sttrans.h。
2 代码简析
802.1w标准对应RSTP,802.1d标准对应STP,针对802.1w和802.1d标准,交换机芯片硬件一般提供不同形式的API:
(1) 802.1w:对于某个端口定义“学习”、“转发”两种状态的使能和禁止,对于“丢弃”状态,将“学习”和“转发”状态同时禁止即可;
#ifdef
STRONGLY_SPEC_802_1W
static
Bool disableLearning (STATE_MACH_T *this)
{
register PORT_T *port = this->owner.port;
return STP_OUT_set_learning
(port->port_index, port->owner->vlan_id, False);
}
static
Bool disableLearning (STATE_MACH_T *this) { }
static
Bool enableLearning (STATE_MACH_T *this) { }
static
Bool disableForwarding (STATE_MACH_T *this) { }
static
Bool enableForwarding (STATE_MACH_T *this) { }
#endif
(2) 802.1d:对于某个端口定义“关闭”、“阻塞”、“侦听”、“学习”、“转发”5种状态,提供2个比特,映射到5种状态,其中“关闭”、“阻塞”、“侦听”一般可应用为“丢弃”。
typedef
enum {

UID_PORT_DISABLED = 0,
UID_PORT_DISCARDING,
UID_PORT_LEARNING,
UID_PORT_FORWARDING,
UID_PORT_NON_STP
}
RSTP_PORT_STATE;
STP_OUT_set_port_state(port->port_index,
port->owner->vlan_id, UID_PORT_DISCARDING);

此状态机包含以下三态:
#define
STATES { /
CHOOSE(DISCARDING), /
CHOOSE(LEARNING), /
CHOOSE(FORWARDING), /
}
初始化进入DISCARDING状态,各状态之间根据当前状态结合port->learn、port->forward标识进行倒换(STP_sttrans_check_conditions(
)),进入某状态后都有固定的动作(STP_sttrans_enter_state(
)),这里主要是打开关闭端口的学习和转发功能。

/* 状态执行动作 */
void STP_sttrans_enter_state
(STATE_MACH_T *this)
{
register PORT_T *port = this->owner.port;
switch (this->State) {
case BEGIN:
case DISCARDING:
port->learning = False;
port->forwarding = False; // 更新标识
#ifdef STRONGLY_SPEC_802_1W // 802.1w
disableLearning (this);
disableForwarding (this); // 禁止学习和转发,效果上即执行“丢弃”
#else // 802.1d
STP_OUT_set_port_state
(port->port_index, port->owner->vlan_id, UID_PORT_DISCARDING); // 将端口状态设为“丢弃”
#endif
break;

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: