Bridge implementation of the linux protocol stack (from the bridge configuration (2))

  
 

About the bridge: The bridge is a Layer 2 device that can be considered a Layer 2 switch before going deeper. It forwards data on a Layer 2 protocol. In order to forward data, the bridge maintains a correspondence table between the port and the MAC, usually as a CAM table. According to this table, the data can be sent to the corresponding port for transmission. The forwarding process of the bridge is: 1: Receive a packet. Determine whether the CAM table contains the source address of the package. If not, update the source address and port to the CAM table. 2: Determine whether the packet is sent to the machine, and if so, send it to the upper layer of the machine. Protocol stack processing. If not, look up the CAM table. Find the appropriate exit. 3: If an exit is found, the package is sent to the exit. If it does not exist, it will be sent on each port. 4: If the corresponding entry in the CAM table is not updated within the specified time, delete this item. Bridge configuration: Brctl is a good tool for configuring bridges. Its source code and configuration method is extremely simple. We will talk about the configuration process of the bridge and see how the Linux kernel is managed step by step. First, create a bridge: brctl addbr br0 (create a bridge for br0) Then, add the interface to the bridge: brctl addif br0 eth0 (add eth0 and eth1 to bridge br0) brctl addif bro eth1OK, bridge now Just configured. This linux host can be used as a switch, and packets from eth0 can be forwarded to eth1. Now, let's take a look at how the code handles the first brctl addbr . Looking at the brctl code, it calls: ioctl(br_socket_fd, SIOCBRADDBR, brname); then brctl addif is called in the brctl code: ioctl(br_socket_fd, SIOCBRADDIF, &ifr); Brctl's code is very simple, just call the user space configuration tool ioctl.Linux bridge analysis: Well, now you can enter the kernel analysis bridge mode: static int __init br_init(void) (net/brige/br. c) {//allocation slab buffer br_fdb_init (); //bridge netfiter processing, will be analyzed in the following chapters #ifdef CONFIG_BRIDGE_NETFILTERif (br_netfilter_init ()) return 1; #endif //user space ioctl call function brioctl_set (br_ioctl_deviceless_stub); //Receive the processing of the packet, that is, we see in the above netif_receive_skb function br_handle_frame_hookbr_handle_frame_hook = br_handle_frame; #if defined (CONFIG_ATM_LANE)

Copyright © Windows knowledge All Rights Reserved