Preliminary analysis of the Linux Bridge Source Code framework

Source: Internet
Author: User
A preliminary analysis of the Linux Bridge Source Code framework-Linux Enterprise Application-Linux server application information. The following is a detailed description. Today, I encountered a lot of trouble in dealing with the STP problem of the bridge. I have read a lot about this theory. I didn't really learn its source theory. Looking at the implementation of Linux, I have no information at hand, after two hours, I only read the frame structure of the bridge. So I want to post it first. I hope that some of my friends will study it and continue to write it. Let's take a look at it:

Version: Linux 2.4.18

I. Call
In the static void net_rx_action (struct softirq_action * h) Soft Interrupt function of src/net/core/dev. c:
Line 1479

# If defined (CONFIG_BRIDGE) | defined (CONFIG_BRIDGE_MODULE)
If (skb-> dev-> br_port! = NULL &&
Br_handle_frame_hook! = NULL ){
Handle_bridge (skb, pt_prev );
Dev_put (rx_dev );
Continue;
}
# Endif
If the bridge or bridge module is defined, the handle_bridge function will handle it.
Skb-> dev-> br_port: the port that receives the packet is a member of the bridge port group.
Br_handle_frame_hook: defines the bridge processing function.

Ii. Initialization
Src/net/bridge/br. c:
Static int _ init br_init (void)
{
Printk (KERN_INFO "NET4: Ethernet Bridge 008 for NET4.0 \ n ";

Br_handle_frame_hook = br_handle_frame;
Br_ioctl_hook = br_ioctl_deviceless_stub;
# If defined (CONFIG_ATM_LANE) | defined (CONFIG_ATM_LANE_MODULE)
Br_fdb_get_hook = br_fdb_get;
Br_fdb_put_hook = br_fdb_put;
# Endif
Register_netdevice_notifier (& br_device_notifier );

Return 0;
}
The initialization function indicates that the bridge processing function is br_handle_frame.
Ioctl processing function: br_ioctl_deviceless_stub

Iii. br_handle_frame (br_input.c)
/* Bridge processing function */
Void br_handle_frame (struct sk_buff * skb)
{
Struct net_bridge * br;
Unsigned char * dest;
Struct net_bridge_port * p;

/* Obtain the target MAC address */
Dest = skb-> mac. ethernet-> h_dest;

/* Skb-> dev-> br_port is used to specify the port for receiving the data packet. If it is not a port of the bridge, it is NULL */
P = skb-> dev-> br_port;
If (p = NULL)/* The port is not in the bridge group port */
Goto err_nolock;

/* The bridge group to which the current port belongs */
Br = p-> br;

/* Lock. Because the CAM table needs to be read during forwarding, you must apply a read lock to avoid another kernel control path in this process (such as system calls on another CPU on multiple processors) modify a CAM table */
Read_lock (& br-> lock );
If (skb-> dev-> br_port = NULL)/* previously determined */
Goto err;

/* Br-> dev is the virtual network card of the bridge. If it is not UP, or the bridge is DISABLED, p-> state is actually the status after the STP calculation of the current port of the bridge */
If (! (Br-> dev. flags & IFF_UP) |
P-> state = BR_STATE_DISABLED)
Goto err;

/* The source MAC address is 255. X, that is, the source MAC address is multicast or broadcast, discarded */
If (skb-> mac. ethernet-> h_source [0] & 1)
Goto err;

/* Around the world, the bridge is more intelligent than the HUB because it has a MAC-PORT table, so that no broadcast is required for data forwarding, check the table and set the port.
Every time a packet is received, the bridge learns its source MAC and adds it to this table. In Linux, this table is called a CAM table (this name is shown in other documents ).
If the status of the bridge is LEARNING or FORWARDING (LEARNING or FORWARDING), the source address of the packet is skb-> mac. ethernet-> h_source,
Add it to the CAM table. If it already exists in the table, update the timer. br_fdb_insert completes this process */
If (p-> state = BR_STATE_LEARNING |
P-> state = BR_STATE_FORWARDING)
Br_fdb_insert (br, p, skb-> mac. ethernet-> h_source, 0 );

/* The BPDU packet of the STP protocol uses the multicast target MAC address: Starting from 01-80-c2-00-00-00 (Bridge_group_addr: bridge multicast address ).
Therefore, if STP is enabled, and the current data packet is a BPDU
(! Memcmp (dest, bridge_ula, 5), unsigned char bridge_ula [6] = {0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 },
Then it is processed by the corresponding function */
If (br-> stp_enabled &&
/* Only the first five bytes are compared here. I have not carefully studied that STP uses all multicast addresses (from 0 1: 0 0: 5 e: 0 0: 0 0: 0: 0 0 to 0 1: 0 0: 5 e: 7 f: ff .), I used only one part. It seems that it is only one part. I didn't go into details */
! Memcmp (dest, bridge_ula, 5 )&&
! (Dest [5] & 0xF0)/* 01-80-c2-00-00-F0 what is the address? Why do we need to judge? */
Goto handle_special_frame;

/* Process the hook function and transfer it to the br_handle_frame_finish function for further processing */
If (p-> state = BR_STATE_FORWARDING ){
NF_HOOK (PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb-> dev, NULL,
Br_handle_frame_finish );
Read_unlock (& br-> lock );
Return;
}

Err:
Read_unlock (& br-> lock );
Err_nolock:
Kfree_skb (skb );
Return;

Handle_special_frame:
If (! Dest [5]) {
Br_stp_handle_bpdu (skb );
Return;
}

Kfree_skb (skb );
}

4. br_handle_frame_finish

Static int br_handle_frame_finish (struct sk_buff * skb)
{
Struct net_bridge * br;
Unsigned char * dest;
Struct net_bridge_fdb_entry * dst;
Struct net_bridge_port * p;
Int passedup;

/* The previous steps are basically the same */
Dest = skb-> mac. ethernet-> h_dest;


P = skb-> dev-> br_port;
If (p = NULL)
Goto err_nolock;

Br = p-> br;
Read_lock (& br-> lock );
If (skb-> dev-> br_port = NULL)
Goto err;

Passedup = 0;

/* If the network card of the bridge is in the hybrid mode, clone each received packet.
Sent to the AF_PACKET protocol processing body (processing of the ptype_all chain in the net_rx_action soft network interrupt function ). */
If (br-> dev. flags & IFF_PROMISC ){
Struct sk_buff * skb2;

Skb2 = skb_clone (skb, GFP_ATOMIC );
If (skb2! = NULL ){
Passedup = 1;
Br_pass_frame_up (br, skb2 );
}
}

/* If the target MAC is broadcast or multicast, the packet needs to be sent to the upper-layer protocol stack of the Local Machine. Here there is a flag variable passedup
Indicates whether the message has been sent. If the message has been sent, forget it */
If (dest [0] & 1 ){
Br_flood_forward (br, skb ,! Passedup );
If (! Passedup)
Br_pass_frame_up (br, skb );
Goto out;
}

/* The MAC-PORT table in Linux is a CAM table. Here, we look up the table based on the destination address to determine which interface forwards the packet.
Each table item is described by the structure struct net_bridge_fdb_entry:
Struct net_bridge_fdb_entry
{
Struct net_bridge_fdb_entry * next_hash; // linked list pointer for CAM table connection
Struct net_bridge_fdb_entry ** pprev_hash; // Why is pprev not prev? I have not studied it carefully
Atomic_t use_count; // current reference counter of this item
Mac_addr addr; // MAC address
Struct net_bridge_port * dst; // The physical port corresponding to this entry
Unsigned long ageing_timer; // process MAC timeout
Unsigned is_local: 1; // whether it is the MAC address of the Local Machine
Unsigned is_static: 1; // whether it is a static MAC address
};*/
Dst = br_fdb_get (br, dest );

/* After querying the CAM table, if the table item can be found and the target MAC is directed to the virtual network card of the Local Machine, submit the package to the upper-layer protocol,
In this way, we can remotely manage the bridge through the address of this virtual network card */
If (dst! = NULL & dst-> is_local ){
If (! Passedup)
Br_pass_frame_up (br, skb );
Else
Kfree_skb (skb );
Br_fdb_put (dst );
Goto out;
}

/* If the table is found and is not a local virtual network card, forward it */
If (dst! = NULL ){
Br_forward (dst-> dst, skb );
Br_fdb_put (dst );
Goto out;
}

/* If the table cannot be found, you have to learn the HUB ...... */
Br_flood_forward (br, skb, 0 );

Out:
Read_unlock (& br-> lock );
Return 0;

Err:
Read_unlock (& br-> lock );
Err_nolock:
Kfree_skb (skb );
Return 0;
}

This is the basic framework. It is similar to those mentioned in the books about the bridge principle ......
The bridge is mainly based on the two functions:
Br_fdb_insert
Br_fdb_get
One learning and one look-up table;
In addition, STP is supported to process BPDU. The br_stp_handle_bpdu function is required.
Which one has detailed analysis of these three functions? Can I send a copy of the code to help you avoid the effort of the afternoon ......

After scanning br_fdb_insert, the structure is still very clear. If the current item already exists in the hash table, update it (_ fdb_possibly_replace). If it is a new item, insert it, it is actually a two-way linked list maintenance process (_ hash_link ):

Void br_fdb_insert (struct net_bridge * br,
Struct net_bridge_port * source,
Unsigned char * addr,
Int is_local)
{
Struct net_bridge_fdb_entry * fdb;
Int hash;

Hash = br_mac_hash (addr );

Write_lock_bh (& br-> hash_lock );
Fdb = br-> hash [hash];
While (fdb! = NULL ){
If (! Fdb-> is_local &&
! Memcmp (fdb-> addr. addr, addr, ETH_ALEN )){
_ Fdb_possibly_replace (fdb, source, is_local );
Write_unlock_bh (& br-> hash_lock );
Return;
}

Fdb = fdb-> next_hash;
}

Fdb = kmalloc (sizeof (* fdb), GFP_ATOMIC );
If (fdb = NULL ){
Write_unlock_bh (& br-> hash_lock );
Return;
}

Memcpy (fdb-> addr. addr, addr, ETH_ALEN );
Atomic_set (& fdb-> use_count, 1 );
Fdb-> dst = source;
Fdb-> is_local = is_local;
Fdb-> is_static = is_local;
Fdb-> ageing_timer = jiffies;

_ Hash_link (br, fdb, hash );

Write_unlock_bh (& br-> hash_lock );
}

Similarly, the lookup table is also a traversal table that performs address matching:
Struct net_bridge_fdb_entry * br_fdb_get (struct net_bridge * br, unsigned char * addr)
{
Struct net_bridge_fdb_entry * fdb;

Read_lock_bh (& br-> hash_lock );
Fdb = br-> hash [br_mac_hash (addr)];
While (fdb! = NULL ){
If (! Memcmp (fdb-> addr. addr, addr, ETH_ALEN )){
If (! Has_expired (br, fdb )){
Atomic_inc (& fdb-> use_count );
Read_unlock_bh (& br-> hash_lock );
Return fdb;
}

Read_unlock_bh (& br-> hash_lock );
Return NULL;
}

Fdb = fdb-> next_hash;
}

Read_unlock_bh (& br-> hash_lock );
Return NULL;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.