Netruon understanding (12): using Linux Bridge to connect the Linux network namespace to the extranet

Source: Internet
Author: User
Tags openvswitch
<span id="Label3"></p><p><p>Learn Neutron Series Articles:</p></p><p><p>(1) Virtualization Network implemented by Neutron</p></p><p><p>(2) Neutron Openvswitch + VLAN Virtual network</p></p><p><p>(3) Neutron Openvswitch + gre/vxlan Virtual network</p></p><p><p>(4) Neutron OVS OpenFlow flow table and L2 Population</p></p><p><p>(5) Neutron DHCP Agent</p></p><p><p>(6) Neutron L3 Agent</p></p><p><p>(7) Neutron LBaas</p></p><p><p>(8) Neutron Security Group</p></p><p><p>(9) Neutron FWaas and Nova Security Group</p></p><p><p>(ten) Neutron Vpnaas</p></p><p><p>(one) Neutron DVR</p></p><p><p>(Neutron VRRP)</p></p><p><p>(+) High Availability (HA)</p></p><p><p>(14) Connect the Linux network namespace to the extranet using NAT</p></p><p><p>(15) using Linux Bridge to connect the Linux network namespace to the external network</p></p><p><p></p></p><p><p>The previous article introduced the use of NAT to connect the Linux network namespace to the extranet, but this model has a lot of limitations, including that it uses an internal ip, so the external computer does not have direct access to its IP and needs to access it by accessing its host and then through DNAT. Its application scenario is usually because the enterprise uses the public network IP address is generally limited, when the internal computer needs to access the public network, often to take a NAT Approach. This article describes the use of Linux Bridge to connect the Linux network namespace to the external Network.</p></p>1. Environment and Configuration 1.1 test environment<p><p>(fig. 1)</p></p><p><p>Environment description:</p></p> <ul> <ul> <li>Computer 1 (host1) is a VMware virtual machine on ESXi that has two network cards, eth0 used to communicate with computer 2 (host2).</li> <li>The IP address of the host1 is 192.168.1.87. Another machine used by the experiment host2 the IP for 192.168.1.48. Their gateways are 192.168.1.1.</li> <li>The/proc/sys/net/ipv4/ip_forward content on the host1 is 0, which means the IP forwarding is off</li> <li>Iptables Filter table on Host1:</li> </ul> </ul><pre><pre>[email protected]:/etc# iptables-t filter-<span style="color: #000000;">S</span>-<span style="color: #000000;">p INPUT Accept</span>-<span style="color: #000000;">p FORWARD DROP</span>-p OUTPUT Accept</pre></pre><p><p><span style="line-height: 1.5;">In order to be able to ping host2 from the Netns ns2 on the host1, you need to do the configuration and instructions:</span></p></p> <table border="0"> <tbody> <tr> <td><span style="font-size: 14px;"><strong>Steps</strong></span></td> <td><span style="font-size: 14px;"><strong>Command</strong></span></td> <td><span style="font-size: 14px;"><strong>Description</strong></span></td> </tr> <tr> <td><span style="font-size: 14px;">1</span></td> <td><span style="font-size: 14px;">IP netns Add ns2</span></td> <td><span style="font-size: 14px;">Create a Linux network namespace named ' Ns2 '</span></td> </tr> <tr> <td><span style="font-size: 14px;">2</span></td> <td><span style="font-size: 14px;">IP link Add veth1 type Veth peer name Veth2</span></td> <td><span style="font-size: 14px;">Create a Veth device, one end is veth1, the other is Veth2</span></td> </tr> <tr> <td><span style="font-size: 14px;">3</span></td> <td><span style="font-size: 14px;">IP link set veth2 netns ns2</span></td> <td><span style="font-size: 14px;">Add Veth2 to NS2 as one of its network interface</span></td> </tr> <tr> <td><span style="font-size: 14px;">4</span></td> <td><p><span style="font-size: 14px;">Brctl ADDBR BR1</span></p><p><span style="font-size: 14px;">Brctl addif eth0 BR1</span></p><p><span style="font-size: 14px;">Ifconfig eth0 0.0.0.0</span></p><p><span style="font-size: 14px;">Ifconfig BR1 192.168.1.87/24 up</span></p><p><span style="font-size: 14px;">Brctl addif BR1 veth1</span></p></td> <td><p><span style="font-size: 14px;">Create Liux Bridge</span> <span style="font-size: 14px;">' BR1 '</span></p><p><span style="font-size: 14px;">Remove the IP address of the eth0 and set its address to BR1</span></p><p><span style="font-size: 14px;">Add eth0 to BR1</span></p><p><span style="font-size: 14px;">Add Veth1 to BR1</span></p></td> </tr> <tr> <td><span style="font-size: 14px;">5</span></td> <td><span style="font-size: 14px;">IP netns exec ns2 ifconfig veth2 192.168.1.88/24 up</span></td> <td><span style="font-size: 14px;">Configure the IP address of the veth2, which is on the same network segment as Host1 and Host2</span></td> </tr> <tr> <td><span style="font-size: 14px;">6</span></td> <td><span style="font-size: 14px;">IP netns exec ns2 route add default GW 192.168.1.1</span></td> <td><span style="font-size: 14px;">Set the default route for NS2 to the gateway address of Host1 and Host2</span></td> </tr> <tr> <td><span style="font-size: 14px;">7</span></td> <td><p><span style="font-size: 14px;">Iptables-t filter-a forward-m physdev--physdev-in eth0--physdev-out veth1-j ACCEPT</span></p><p><span style="font-size: 14px;">Iptables-t filter-a forward-m physdev--physdev-out eth0--physdev-in veth1-j ACCEPT</span></p></td> <td><span style="font-size: 14px;">Set FORWARD Rules</span></td> </tr> </tbody> </table><p><p>After you have done the above configuration,</p></p> <ul> <ul> <li>You can ping other computers from the ns2, whether it is a 192.168.1.0 network segment or another network segment</li> <li>You can ping 192.168.1.88 from another computer</li> </ul> </ul>2. Principle 2.1 Linux Bridge forwarding<p><p>When you ping the IP address of veth2 from an external computer iptables statistics:</p></p><pre>Chain INPUT (policy ACCEPT<span style="color: #0000ff;"><span style="color: #0000ff;">1814</span></span><span style="color: #000000;"><span style="color: #000000;">packets, 198K bytes) pkts bytes target prot opt</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">inch</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> out</span><span style="color: #000000;"><span style="color: #000000;">source Destinationchain FORWARD (policy DROP</span></span><span style="color: #800080;"><span style="color: #800080;"></span> the</span>Packets<span style="color: #800080;"><span style="color: #800080;">7950</span></span><span style="color: #000000;"><span style="color: #000000;">bytes) pkts bytes target prot opt</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">inch</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> out</span><span style="color: #000000;"><span style="color: #000000;">Source Destination</span></span><span style="color: #800080;"><span style="color: #800080;">154</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">11794</span></span>ACCEPT all--* *<span style="color: #800080;"><span style="color: #800080;">0.0</span></span>.<span style="color: #800080;"><span style="color: #800080;">0.0</span></span>/<span style="color: #800080;"><span style="color: #800080;">0</span></span> <span style="color: #800080;"><span style="color: #800080;">0.0</span></span>.<span style="color: #800080;"><span style="color: #800080;">0.0</span></span>/<span style="color: #800080;"><span style="color: #800080;">0</span></span>Physdev match--physdev-<span style="color: #0000ff;"><span style="color: #0000ff;">inch</span></span>ens9f0--physdev-<span style="color: #0000ff;"><span style="color: #0000ff;"></span> out</span><span style="color: #000000;"><span style="color: #000000;">veth1</span></span><span style="color: #800080;"><span style="color: #800080;">121</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">8724</span></span>ACCEPT all--* *<span style="color: #800080;"><span style="color: #800080;">0.0</span></span>.<span style="color: #800080;"><span style="color: #800080;">0.0</span></span>/<span style="color: #800080;"><span style="color: #800080;">0</span></span> <span style="color: #800080;"><span style="color: #800080;">0.0</span></span>.<span style="color: #800080;"><span style="color: #800080;">0.0</span></span>/<span style="color: #800080;"><span style="color: #800080;">0</span></span>Physdev match--physdev-<span style="color: #0000ff;"><span style="color: #0000ff;">inch</span></span>veth1--physdev-<span style="color: #0000ff;"><span style="color: #0000ff;"></span> out</span><span style="color: #000000;"><span style="color: #000000;">ens9f0chain OUTPUT (policy ACCEPT</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> the</span><span style="color: #000000;"><span style="color: #000000;">packets, 184K bytes) pkts bytes target prot opt</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">inch</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> out</span>Source destination</pre><p><p>This means that network traffic does go through the Linux bridge to do Forwarding. For more details on the description of the Iptables settings on Linux bridge, please refer to ebtables/iptables interaction on a linux-based bridge. Highlights Include:</p></p> <ul> <ul> <ul> <li>Iptables and Ebtables filter tables related to Linux Bridge</li> <li>The Linux kernel 2.6 version contains the code for Ebtables and br-nf, where the BR-NF code makes the network frame/packet of Bridge pass through the iptables filter table; ebtables table works on the network two layer (Ethernet), iptables The table works on the upper layer (IP).</li> <li>When an IP packet enters a bridge/router interface, it sends the network packet to another bridge interface in the following scenarios:</li> <li>(fig. 2)</li> <li>When an IP packet enters a bridge/router interface, it sends the network packet to a Non-bridge interface case as Follows: <ul> <li>(fig. 3)</li> </ul></li> </ul> </ul> </ul><p><p>Based on the above principles, the requirements of the case in this paper,</p></p><p><p>(1) If you use the Physdev module, you can use the following two rules for precise control:</p></p><pre><pre>IPTABLES-T filter -A forward-m physdev--physdev-<span style="color: #0000ff;"></span> in eth0--physdev-<span style="color: #0000ff;"></span> out veth1-<span style="color: #000000;"></span>-t filter< C7/>-a forward-m physdev--physdev-<span style="color: #0000ff;"></span> out eth0--physdev-<span style="color: #0000ff;"></span> in Veth1-j ACCEPT</pre></pre><p><p>Note:</p></p> <ul> <ul> <li>physdev-in specifies the port of the bridge to which the network packet enters</li> <li>PHYSDEV-OUT specifies the port of the bridge to which the network packet left</li> </ul> </ul><p><p>(2) You can also use general rules to control whether BR1 are allowed to be forwarding</p></p><pre><pre>Iptables-t filter-a forward-i br1-j ACCEPT</pre></pre><p><p>(3) the IP forwrading of the Linux kernel does not need to be enabled, it is primarily for NAT Scenarios.</p></p>2.2 The forwarding behavior of Linux Bridge<p><p>Linux Bridge determines its behavior based on the destination MAC address of the received Frame:</p></p> <ul> <ul> <li>If the destination MAC address of the frame is known at the other end of bridge, it will do bridging (forwarding)</li> <li>If the destination MAC address of the frame is unknown, it will do flooding (flooding)</li> <li>If the destination MAC address of a frame is the MAC address (local) of Bridge itself or one of its ports, it is sent to the upper IP layer</li> <li>If the destination MAC address is known and is on the source side of bridge, Discard</li> </ul> </ul><p><p>Linux Bridge itself maintains a mapping table for MAC addresses and input ports:</p></p><pre>[email protected]:/home/s1# brctl Showmacs br-<span style="color: #000000;"><span style="color: #000000;">Ens9f0port no Mac addr</span></span><span style="color: #0000ff;">is <span style="color: #0000ff;">local?</span></span><span style="color: #000000;"><span style="color: #000000;">Ageing Timer</span></span> <span style="color: #800080;"><span style="color: #800080;">1</span></span> <span style="color: #800080;"><span style="color: #800080;"></span> -</span>: 0e:d5:c6:<span style="color: #800080;"><span style="color: #800080;"></span> -</span>:<span style="color: #800080;"><span style="color: #800080;"></span> -</span>No<span style="color: #800080;"><span style="color: #800080;">224.96</span></span> <span style="color: #800080;"><span style="color: #800080;">2</span></span>7e:<span style="color: #800080;"><span style="color: #800080;"></span> -</span>:<span style="color: #800080;"><span style="color: #800080;"></span> -</span>: bb:<span style="color: #800080;"><span style="color: #800080;"></span> +</span>:<span style="color: #800080;"><span style="color: #800080;"></span> the</span>Yes<span style="color: #800080;"><span style="color: #800080;">0.00</span></span> <span style="color: #800080;"><span style="color: #800080;">2</span></span>7e:<span style="color: #800080;"><span style="color: #800080;"></span> -</span>:<span style="color: #800080;"><span style="color: #800080;"></span> -</span>: bb:<span style="color: #800080;"><span style="color: #800080;"></span> +</span>:<span style="color: #800080;"><span style="color: #800080;"></span> the</span>Yes<span style="color: #800080;"><span style="color: #800080;">0.00</span></span> <span style="color: #800080;"><span style="color: #800080;">1</span></span>A0:<span style="color: #800080;"><span style="color: #800080;"></span> $</span>: 9f:5c:<span style="color: #800080;"><span style="color: #800080;"></span> the</span>: F8 No<span style="color: #800080;"><span style="color: #800080;">58.07</span></span> <span style="color: #800080;"><span style="color: #800080;">1</span></span>A0:<span style="color: #800080;"><span style="color: #800080;"></span> $</span>: 9f:<span style="color: #800080;"><span style="color: #800080;"></span> the</span>: b1:<span style="color: #800080;"><span style="color: #800080;"></span> the</span>Yes<span style="color: #800080;"><span style="color: #800080;">0.00</span></span> <span style="color: #800080;"><span style="color: #800080;">1</span></span>A0:<span style="color: #800080;"><span style="color: #800080;"></span> $</span>: 9f:<span style="color: #800080;"><span style="color: #800080;"></span> the</span>: b1:<span style="color: #800080;"><span style="color: #800080;"></span> the</span>Yes<span style="color: #800080;"><span style="color: #800080;">0.00</span></span> <span style="color: #800080;"><span style="color: #800080;">2</span></span>Ee:<span style="color: #800080;"><span style="color: #800080;"></span> the</span>:<span style="color: #800080;"><span style="color: #800080;"></span> to</span>: Cc:<span style="color: #800080;"><span style="color: #800080;"></span> the</span>:<span style="color: #800080;"><span style="color: #800080;"></span> one</span>No<span style="color: #800080;"><span style="color: #800080;">0.29</span></span></pre> <ul> <ul> <li>When you ping an external computer from ns2, because Bridge discovers that its destination MAC address is not local, it executes the Linux bridging on that IP packet, which triggers the filtering rules discussed in 2.1.</li> <li>When Ping host1 from ns2, because the destination MAC address is local, it will be sent directly to the IP layer, at this time, the previous discussion of the FORWARD chain filter table Two filtering rules do not work on it, which is why even if you do not add the Two-day rule, ping Host1 will also Succeed.</li> </ul> </ul>2.3 Promiscuous mode of the network interface (promiscuous mode, abbreviated as Promisc Mode)<p><p>According to wikipedia, the PROMISC model refers to a NIC that gives all the network traffic it accepts to the cpu, rather than just handing over the part it wants to transfer to the CPU. In an IEEE 802 network, each network frame has a destination MAC address. In Non-promiscuous mode, the NIC will only accept unicast frames with the destination MAC address as its own Mac address, as well as multicast and broadcast Frames. In promiscuous mode, the NIC accepts all frames that pass through it.</p></p><p><p>simply, You can use the Ifconfig command or the netstat-i command to see if the promiscuous mode on a network interface is open:</p></p> <ul> <ul> <li>Ifconfig eth0, View the configuration of the eth0, including promiscuous Mode. When the output contains prmisc, it indicates that the network interface is in promiscuous Mode. But there are also special cases, see the 2.3.2 section Below.</li> </ul> </ul><pre><pre>[email protected]:/home/<span style="color: #000000;">s1# ifconfig eth0eth0 Link encap:ethernet HWaddr a0:</span>9f:<span style="color: #800080;"></span><span style="color: #800080;">97 </span>: b1:<span style="color: #800080;"></span><span style="color: #000000;"> inet6 addr:fe80::a236:9fff:fe97:b168</span>/<span style="color: #800080;"></span> <span style="color: #000000;"> scope:link <span style="color: #0000ff;"> Promisc</span> multicast MTU:</span><span style="color: #800080;"></span> Metric:<span style="color: #800080;">1</span><span style="color: #000000;"><br></span></pre></pre> <ul> <ul> <li>Ifconfig eth0 promisc//make eth0 into promiscuous mode</li> <li>Ifconfig eth0-promisc//make eth0 exit promiscuous mode</li> </ul> </ul><p><p>In this example, after eth0 network traffic, including to it, but also to the ns2, the destination MAC address of the frame is no longer the Eth0 own Mac address, therefore, eth0 promiscuous mode must be turned On. however, during the testing process, two interesting things were found.</p></p>2.3.1 Promiscuous mode of NIC for VMware virtual machine<p><p>According to how promiscuous mode works at the virtual switch and portgroup levels this article, the VMware virtual machine NIC needs to enable promiscuous mode when VMware VSwitch and its The Vport in Virtual group also needs to turn on promiscuous Mode. When VSwitch is enabled for promiscuous mode, all the port groups (portgroup) above it also turn on promiscuous mode by Default. well, in my test, it took me quite a while to find this Problem.</p></p><p><p>(1) turn on the promiscuous mode of VSwitch</p></p><p><p></p></p><p><p>(2) the promiscuous mode of the port group, The default is the promiscuous mode with vSwitch, but you can also explicitly open or close</p></p><p><p></p></p>2.3.2 Hybrid mode for Linux bridge and its interface<p><p>The testing process found that when a network interface is added to the Linux bridge, its promiscuous mode is automatically turned on and cannot be closed until it is removed from Bridge.</p></p><p><p>(1) automatically exits promiscuous mode when the Veth device leaves the Linux bridge</p></p><pre><pre>Brctl delif br-<span style="color: #000000;"></span>| <span style="color: #000000;">grep promiscuous[</span><span style="color: #800080;">498665.637647</span><span style="color: #0000ff;"></span> left promiscuous mode</pre></pre><p><p>You can manually set its Promisc mode now:</p></p><pre>[email protected]:/home/s1# Netstat-i |<span style="color: #000000;"><span style="color: #000000;">grep veth40veth40</span></span><span style="color: #800080;"><span style="color: #800080;"></span> the</span> <span style="color: #800080;"><span style="color: #800080;">0</span></span> <span style="color: #800080;"><span style="color: #800080;">68386</span></span> <span style="color: #800080;"><span style="color: #800080;">0</span></span> <span style="color: #800080;"><span style="color: #800080;">0</span></span> <span style="color: #800080;"><span style="color: #800080;">0</span></span> <span style="color: #800080;"><span style="color: #800080;">280670</span></span> <span style="color: #800080;"><span style="color: #800080;">0</span></span> <span style="color: #800080;"><span style="color: #800080;">0</span></span> <span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">Bmru[email protected]:</span></span>/home/<span style="color: #000000;"><span style="color: #000000;">s1# ifconfig veth40 promisc[email protected]:</span></span>/home/s1# Netstat-i |<span style="color: #000000;"><span style="color: #000000;">grep veth40veth40</span></span><span style="color: #800080;"><span style="color: #800080;"></span> the</span> <span style="color: #800080;"><span style="color: #800080;">0</span></span> <span style="color: #800080;"><span style="color: #800080;">68386</span></span> <span style="color: #800080;"><span style="color: #800080;">0</span></span> <span style="color: #800080;"><span style="color: #800080;">0</span></span> <span style="color: #800080;"><span style="color: #800080;">0</span></span> <span style="color: #800080;"><span style="color: #800080;">280670</span></span> <span style="color: #800080;"><span style="color: #800080;">0</span></span> <span style="color: #800080;"><span style="color: #800080;">0</span></span> <span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">BM<span style="color: #0000ff;">P</span>ru[</span></span><span style="color: #800080;"><span style="color: #800080;">498822.803260</span></span>] Device Veth40 entered promiscuous mode</pre><p><p>(2) automatically enter Promisc mode when the Veth device is added to Linux bridge</p></p><pre><pre>Brctl addif br-<span style="color: #000000;"></span>| <span style="color: #000000;">grep promiscuous[</span><span style="color: #800080;">498681.199680</span>] device Veth40 entered promiscuous mode</pre></pre><p><p>Even if it is set to Non-promisc mode through ifconfig, Netstat-i also shows that it is in Non-promis mode, but it is still in Promisc mode at this point:</p></p><pre><pre></pre></pre><pre><pre><span>DMESG No RECORDS.</span></pre></pre><pre><pre><span style="color: #000000;">[email protected]:</span>/home/s1# ifconfig veth40-<span style="color: #000000;"><br>[email protected]:</span>/home/s1# netstat-i |<span style="color: #000000;"><br></span><span style="color: #800080;"></span> the <span style="color: #800080;">0</span> <span style="color: #800080;">68410</span> <span style="color: #800080;">0</span> <span style="color: #800080;">0</span> <span style="color: #800080;">0</span> <span style="color: #800080;">280723</span> <span style="color: #800080;">0</span> <span style="color: #800080;">0</span> <span style="color: #800080;">0</span> Bmru</pre></pre><p><p>Conclusion:</p></p> <ul> <ul> <li>After the network device is added to Linux bridge, it automatically enters promiscuous mode and cannot exit. It should be said that this approach also simplifies the network configuration in the Linux bridge Environment.</li> <li>After the network device moves out of the Linux bridge, it automatically exits promiscuous mode and can modify</li> <li>Linux Bridge itself is a two-tier network device, but it does not have a promiscuous mode, it will be forwarded according to the situation or flooding network frames</li> </ul> </ul><p><p></p></p><p><p>Note: the relevant knowledge of Linux Bridge and iptables in this paper is more complicated, the author only confirms the test process and results in this paper, and some errors may exist in the theory Analysis. This article will remain updated continuously.</p></p><p><p></p></p><p><p>Netruon understanding (12): using Linux Bridge to connect the Linux network namespace to the extranet</p></p></span>

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.