The implementation analysis of STP in Linux Network Bridge
This article turns from: http://blog.sina.com.cn/s/blog_a3eacdb2010158hn.html
I. The framework structure of STP
STP sends the BPDU package, which has two types: Configuration and TCN (topology change notification);
There are two kinds of processing for BPDU packages: Receive and send (nonsense),
For the configuration type of the BPDU packet sent, it is done by the timer, parameter BPDU packet of several timer parameters;
For the TCP type of BPDU packet sent, from the name can be seen, it is when the topology changes are sent, such as the local bridge configuration changes, physical interface changes, the analysis of other machine changes after the STP package and so on.
BPDU of the package is a IEEE802 package (this would like to put the envelope structure of the picture posted up, can not find where to upload pictures).
Previously analyzed, in the Br_handle_frame function, when the Network Bridge opened the STP, and according to the destination physical address to determine this is an STP package, then to the BR_STP_HANDLE_BPDU function processing.
The BR_STP_HANDLE_BPDU function is primarily to determine what type of BPDU package is, and then call the associated handler function, namely:
if (type==config)
{
BR_RECEIVED_CONFIG_BPDU ();
}
else if (TYPE==TCN)
{
BR_RECEIVED_TCN_BPDU ();
}
This is the process of receiving the BPDU package, about the config type of BPDU packet sent, followed by analysis; TCN Packet sent, there is a part in the receiving package processing process (because of the config type of the BPDU packet, found the topology change, of course, to send TCN package), So here's what we're going to analyze.
Second, config type of BPDU packet receiving processing
This process is done by calling the BR_RECEIVED_CONFIG_BPDU function after the BPDU package has been torn down.
We have to explain some of the theoretical things first:
The STP protocol is ultimately designed to generate a tree without ring in the network, in order to eliminate the effects of broadcast storms and unicast data frames on the network. It is always in the election of three things:
1, root network bridge;
2, the root port;
3, "Specify the port" and "Specify the Network Bridge"
(These three concepts are very important, if you are not clear, it is recommended to consult the relevant documents first, otherwise the following code analysis can not be discussed)
And then according to the election of these three Dongdong, determine the state of the port: blocking, forwarding, learning, listening, disabling ...
To elect these three things, there must be a judgment mark, that is, the algorithm, STP criterion is:
1, to determine the root bridge ID, the smallest for the excellent;
2, to determine the minimum path cost of the root bridge;
3, determine the minimum send hair BID (Sender BID)
4, determine the smallest port ID
If you look at the packet structure of the BPDU, the root bridge ID, the minimum path cost, the sender Network Bridge ID, port ID The concepts should be no problem, but here is a simple one:
1, the root bridge ID, we have configured the Network Bridge, with the BRCTL command will find 8000. XXXxxx such a string, this is the Network Bridge ID number, with a logo for each network bridge, the following XXXX General Bridge MAC address, so that the ID value will not repeat. The root bridge ID is the one with the smallest ID value of all network bridges in the network, and the corresponding bridge with the root bridge ID is also the root bridge of the network;
2. Minimum path overhead
This concept is also used in dynamic routing, but this is not a hop count (LAN is no more than a WAN, not necessarily jump number is slow, such as small number of hops, is 10M link, jump number is a gigabit link, the initial cost is defined as 1000m/chain bandwidth, of course, this way does not apply to million gigabit network ... So then there is a new, each link to define a constant value-please consult the relevant information;
3, send the party ID
Network Bridge to converge before a ring-free topology, you need to send each other BPDU package, of course, you need to tell each other's ID, so that the other side good to compare each other;
4, Port ID
The port ID consists of a priority + port number that identifies a port on a bridge and is easier to use later.
Spanning tree algorithm is to use the above four parameters in the judgment process is always the same:
1, to determine the root bridge, the bridge ID is the smallest (that is, the bridge ID in the package, with its previous record of the smallest bridge ID compared to the machine power, always with its own bridge ID as the root Bridge ID) for the root bridge;
2, determine the minimum path cost;
3, determine the minimum Sender ID;
4, determine the smallest port ID:
These four steps are very important, so the comparison is the four.
With these concepts, let's look at the handling of BPDU packages for config types:
void Br_received_config_bpdu (struct net_bridge_port *p struct BR_CONFIG_BPDU)
{
struct Net_bridge *br;
int was_root;
if (p->state = = br_state_disabled)
Return
BR = p->br;
Read_lock (&br->lock);
Was_root = Br_is_root_bridge (BR);
if (Br_supersedes_port_info (p, BPDU)) {
Br_record_config_information (P, BPDU);
Br_configuration_update (BR);
Br_port_state_selection (BR);
The logical concept of the above paragraph is simple:
1. Compare the parameters in the received BPDU package with their original records (follow the four comparison steps mentioned earlier) to determine whether the update--br_supersedes_port_info (p, BPDU) is required.
2. If the judgment needs to be updated, that is, if any of the above four steps are changed, refresh your own record of saving: Br_record_config_information (P, BPDU);
3, because there is a change, you need to change their configuration: Br_configuration_update (BR); that is, according to the four-step judgment after the election Gan Qiao (note: The root bridge is not elected here, before the text said, it is the timer timed to send BPDU package, Then the received machine only needs to change its own record can), root port, designated port;
4, set the physical port forwarding status: Br_port_state_selection
2.1 Br_supersedes_port_info (p, BPDU)
static int br_supersedes_port_info (struct net_bridge_port *p, struct BR_CONFIG_BPDU)
{
int t;
t = memcmp (&bpdu->root, &p->designated_root,;
if (T < 0)
return 1;
else if (T > 0)
return 0;
if (Bpdu->root_path_cost < p->designated_cost)
return 1;
else if (Bpdu->root_path_cost > P->designated_cost)
return 0;
t = memcmp (&bpdu->bridge_id, &p->designated_bridge,;
if (T < 0)
return 1;
else if (T > 0)
return 0;
if (memcmp (&bpdu->bridge_id, &p->br->bridge_id,)
return 1;
if (bpdu->port_id <= p->designated_port)
return 1;
return 0;
}
2.2 Br_record_config_information
If a change is detected, refresh your records first:
static void Br_record_config_information (struct net_bridge_port *p struct BR_CONFIG_BPDU)
{
P->designated_root = bpdu->root;
P->designated_cost = bpdu->root_path_cost;
P->designated_bridge = bpdu->bridge_id;
P->designated_port = bpdu->port_id;
Br_timer_set (&p->message_age_timer, jiffies-bpdu->message_age);
}
P corresponding to the four members of the concept of the BPDU envelope structure, it is not difficult to understand its meaning:
P->designated_root: The Network Bridge ID of the specified root network bridge
P->designated_cost: