Basic network devices on Linux

Source: Internet
Author: User
The introduction of Linux abstract network devices is similar to that of disk devices. if a Linux user wants to use network functions, the hardware cannot be directly operated, but a Linux user needs to operate directly or indirectly as a device abstracted by us, A common Linux network device. A common situation is:

Introduction to Linux abstract network devices

Similar to a disk device, a Linux user wants to use the network function instead of operating the hardware directly. Instead, the user needs to directly or indirectly operate a Linux device as an abstract device, A common Linux network device. A common situation is that a hardware network card is installed in the system, and Linux will generate a network device instance in the system, such as eth0, you need to issue a command to eth0 to configure or use it. More hardware will bring more device instances, and more virtual hardware will also bring more device instances. With the development of network technology and virtualization technology, more advanced network devices have been added to Linux, making the situation more complex. In the following sections, we will analyze several Linux network device abstraction types frequently used in virtualization technology: Bridge, 802.1.qVLANdevice, VETH, and TAP, explain in detail how to use them in combination with Routetable and IPtable in Linux to create a local virtual network.

How network devices work

Bridge

Bridge is a device used in Linux for layer-2 TCP/IP Protocol Exchange. it functions similar to switches in the real world. A Bridge device instance can be connected to other network device instances on Linux, attach a slave device, similar to a switch in the real world and a user terminal to connect a network cable. When data arrives, Bridge broadcasts, forwards, and discards the packets based on the MAC information.

. Bridge device working process

The Bridge function is mainly implemented in the kernel. When a device is attach to the Bridge, the switch port is inserted with a terminal network cable in the real world. In the kernel program, netdev_rx_handler_register () is called and a callback function for receiving data is registered. This function will be called to forward data to the Bridge whenever the device receives the data. When Bridge receives this data, br_handle_frame () is called to perform a process similar to that of a switch in the real world: Judging the package category (broadcast/single point ), find the internal MAC port ing table, locate the target port number, forward data to the target port or discard it, and automatically update the internal MAC port ing table for self-learning.

There is a difference between Bridge and L2 switches in the real world. The figure on the left shows this situation: data is directly sent to Bridge, rather than being accepted from a port. In this case, the Bridge itself has a MAC that can actively send packets, or the Bridge has a hidden port that is automatically connected to the host Linux system, programs on Linux can send data directly from this port to other ports on the Bridge. So when a Bridge has a network device, for example, when bridge0 is added to eth0, bridge0 actually has two valid MAC addresses: bridge0 and eth0, which can communicate with each other. One interesting thing is that Bridge can set IP addresses. Generally, IP addresses are Layer-3 protocol content and should not appear on the Bridge of a layer-2 device. However, in Linux, Bridge is a type of generic network device Abstraction. as long as it is a network device, an IP address can be set. After a bridge0 has an IP address, Linux can locate bridge0 on the third layer through the route table or IP table rules. in this case, Linux has another hidden virtual network card connected to the hidden port of the Bridge, this Nic is a general network device named bridge0, and the IP address can be regarded as this Nic. When data that meets this IP address reaches bridge0, the kernel protocol stack considers that it has received a packet of data destined for the local machine. in this case, the application can receive the data through Socket. A better example is a vSwitch device with routing in the real world. It also has a hidden MAC address for use by layer-3 protocol processors and hypervisor in the device. The layer-3 protocol processing program in the device corresponds to the layer-3 protocol processing program of a general network device called bridge0, that is, the kernel protocol stack program of the host Linux system. The hypervisor in the device corresponds to the application in the Linux system hosted by bridge0.

Currently, Bridge has a limit: when a device is attach to Bridge, the IP address of that device will become invalid, and Linux will no longer use that IP address to accept data on three layers. For example, if the original IP address of eth0 is 192.168.1.2 and the destination address is 192.168.1.2, the Linux application can accept it through the Socket operation. When eth0 is attach to a bridge0, the application cannot accept the above data even though the IP address of eth0 is still present. In this case, you should assign IP192.168.1.2 to bridge0.

In addition, you must note the direction of the data stream. For a device that is attach to the Bridge, only when it receives the data will the data in this package be forwarded to the Bridge, and then the query table broadcast and other subsequent operations will be completed. When a request is of the sending type, the data is not forwarded to the Bridge, and it looks for the next sending exit. When you configure the network, you often ignore this, resulting in network faults.

VLANdevicefor802.1.q

VLAN, also known as virtual network, is a widely used concept. some applications call their internal networks VLAN. Here, we mainly talk about VLANs that exist in the physical world and require protocol support. There are many different types of VLAN. the protocol principles include MACVLAN, 802.1.qVLAN, 802.1.qbgVLAN, and 802.1.qbhVLAN. 802.1.qVLAN is widely used and mature. The basic principle is to insert additional VLAN protocol data (known as 802.1.qVLANTag) in the L2 Protocol ), it also maintains compatibility with traditional L2 devices. The VLAN device in Linux is an internal software implementation for the 802.1.q protocol, simulating the 802.1.q switch in the real world.

VLAN device operation process

In Linux, 802.1.qVLAN devices are paired with sub-devices. the parent device is equivalent to the TRUNK port of a switch in the real world and is used to connect to the upper-level network. the sub-device is equivalent to a common interface used to connect to the lower-level network. When data is transmitted between the parent and child devices, the kernel performs the corresponding operation according to 802.1.qVLANTag. There is a one-to-many relationship between the parent and child devices. a parent device can have multiple sub-devices, and a sub-device has only one parent device. When a sub-device has a packet of data to be sent, the data is added to the VLANTag and then sent from the parent device. When the parent device receives a packet of data, it analyzes the VLANTag. if a sub-device exists, it forwards the data to the sub-device and removes the VLANTag according to the settings, otherwise, the data is discarded. In some settings, VLANTag can not be removed to meet the needs of some listeners, such as DHCP service programs. Example: eth0 is used as the parent device to create a sub-device eth0.100 with ID 100. If a program requests to send a packet of data from eth0.100, the data will be sent from eth0 with the VLAN100 Tag. If eth0 receives a packet of data and VLANTag is 100, the data will be forwarded to eth0.100, and the VLANTag will be removed based on the settings. If eth0 receives a packet containing VLANTag101, it is discarded. The above process implies the following fact: for the host Linux system, the parent device can only be used to receive data, and the sub-device can only be used to send data. Similar to Bridge, the data of the parent and child devices is also directed. The data received by the sub-device does not enter the parent device, and the data requested by the sub-device is not forwarded to the sub-device. The VLAN parent-child device can be considered as an 802.1.q switch in the real world. sub-interfaces are connected to the host Linux system network through sub-devices, and the parent interface is connected to the parent network through the master device, when the parent device is a physical Nic, the upper-level network is the external real network. when the parent device is another Linux virtual network device, the upper-level network is still the host Linux system network.

Note that the parent and child VLAN devices have the same MAC address and can use it as the MAC address of the 802.1.q switch in the real world. Therefore, multiple VLAN devices share one MAC address. When a parent device has multiple VLAN sub-devices, the sub-devices are isolated and there is no exchange and forwarding relationship like Bridge. The reasons are as follows: 802.1.qVLAN is designed to logically isolate subnets. In the real world, the 802.1.q switch has multiple VLANs. each VLAN has multiple ports. The same VLAN port can be exchanged and forwarded, and different VLAN ports are isolated. Therefore, the 802.1.q switch has two functions: exchange and isolation. LinuxVLANdevice implements the isolation function without the exchange function. A VLAN parent device cannot have two VLAN sub-devices with the same ID, so data exchange is impossible. If you want to connect multiple devices to a VLAN, you need to switch functions. In Linux, Bridge implements the switch function. Therefore, attach VLAN sub-devices to a Bridge to complete the subsequent switch function. To sum up, Bridge and VLANdevice can fully simulate 802.1.q switches in the real world at the functional level.

Linux supports VLAN hardware acceleration. when a specific hardware is installed, the kernel processing process described in the figure can be placed on a physical device.

TAP device and VETH device

A tun/TAP device is a device that allows user-state programs to inject data into the kernel protocol stack. one device works on three layers, one works on two layers, and more is a TAP device. The VETH device appeared earlier, and its function is to reverse the direction of the communication data. the data to be sent will be converted to the data to be received and re-sent to the kernel network layer for processing, this indirectly completes data injection.

Working process of. TAP device and VETH device

When a TAP device is created, a corresponding char device will be generated under the file directory of the Linux device. the user program can open the file to read and write like opening a common file. When the write () operation is performed, the data enters the TAP device. for the Linux network layer, the TAP device receives a packet of data and requests the kernel to accept it, just as a physical network card receives a packet of data from the outside world, the difference is that the data comes from a user program on Linux. After receiving the data, Linux will perform subsequent processing based on the network configuration to complete the data injection function from the user program to the Linux kernel network layer. When a user program executes a read () request, it is equivalent to querying the kernel whether there is any data to be sent out on the TAP device. If yes, it is retrieved into the user program, send data to the TAP device. A metaphor for the image of the TAP device is that the application program using the TAP device is equivalent to another computer, and the TAP device is a local Nic, and they are connected to each other. Applications communicate with the local network core through read ()/write () operations.

The VETH device always appears in pairs, and the data sent to one end of a request always appears in the form of a request accepted from the other end. This device cannot be directly operated by user programs, but it is relatively simple to use. After the data is created and configured correctly, the VETH changes the data direction and sends it to the kernel network core to complete data injection. This data can be read at the other end.

Network Settings

To better describe the usage of Linux network devices, we will use a series of examples to illustrate the data flow in a virtual network composed of complex Linux network elements. Network settings are described as follows: a central Bridge: under bridge0, attach has four network devices, including two VETH devices, one TAP device tap0, and one physical network card eth0. A VLAN sub-device is created at the other end of VETH. There are two VLAN networks in Linux, including vlan100 and vlan200. The physical Eni is connected to an external network, and a VLAN sub-device with a VLANID of 200 is created under it.

Send ARP packets from vlan100 sub-devices

. ARP from vlan100 child device

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.