The Virtual Network Model in linux and the isolation model created using qemu-kvm, linuxqemu-kvm

Source: Internet
Author: User

The Virtual Network Model in linux and the isolation model created using qemu-kvm, linuxqemu-kvm

The first isolation model:

 

Both Guest1 and Guest2 are virtual machines.

First, you need to know that the virtual machine's network adapter in linux contains the first half and the second half, the first half is on the virtual machine, and the second half is on the host machine. Here we take centos6 as an example,

Eth0 is the NIC on the Guest1 virtual machine, and the second half of the corresponding segment is vnet0. All data sent to eth0 on Guest1 is directly sent to vnet0. You can also regard vnet0

One Network Card

How does Guest1 communicate with Guest2?

It is very easy to create a virtual switch in the host machine, so that vnet0 and vnet1 are an interface of the virtual switch, the switch can also be called bridge, as long as two virtual machine NICs

The IP addresses in the first half of the CIDR block can communicate with each other. This is the isolation model.

 

The second routing model:

In the vswitch, The virnet1 Nic is added, and the eth0 and eth1 gateways are directed to virnet1. Then, the core forwarding function of linux is enabled, and the virtual machine can communicate with the external network.

How to enable the core forwarding function sysctl-w net. ipv4.ip _ ipforward = 1

 

The third NAT model:

 

 

In the routing model, although the Virtual Machine ip packets can be sent out to communicate with the external host, the external host may not find the route to the virtual machine but can communicate with the host machine.

The response of the external host can be obtained, and the source address of the ip packet sent to communicate with the external host is converted to the address of the host's physical Nic. This is the NAT model and the most used

Multiple Models

 

The last bridge model:

 

Create a bridge device on the host machine and place eth0 on the bridge. In this way, eth0 on Guest1 sends the packet to vnet0 and then directly sends the packet to eth0 on the host machine.

Change the address to the eth0 address on the host machine.

When the response message arrives at eth0 on the physical machine, how can we determine whether the response message is sent to the virtual machine or the physical machine itself?

Physical opportunities first create a virtual network card and enable the hybrid mode on the Physical Machine (whether the mac address is your own or not). If the mac address is your own

Send it to the virtual network card. If it is not your own, it will be forwarded to vnet0. This is the bridge model. Because the network card of the physical machine has the bridge function, it is called the bridge model.

 

In linux, how does one create a bridge device?

The bridge device is implemented in the kernel

Modinfo bridge check whether this module is installed in the kernel

 

The command for creating a bridge is brctl. Use brctl-h to view the Command help. Use brctl addbr br0 to create a network interface named br0.

 

Brctl show displays all bridges on the current host

 

Ifconfig br0 up activation br0 Interface

 

Qemu-kvm net options

Nic, tap, and user network interface properties. nic is the first half of the VM nic, and tap is the second half of the corresponding

Net nic: Create a new Virtual Machine nic interface. Usage:

-Net nic [, vlan = n] [, macaddr = mac] [, model = type] [, name = name] [, addr = addr] [, vectors = v]

Create a new Nic device and connect it to the specified vlan. macaddr is used to specify the MAC address for the device.

Model specifies the NIC type. qemu can simulate multiple NIC devices, such as virtio. The default NIC In the PC architecture is e1000. You can use "qemu-

Kvm-net nic, model = ?" To obtain the classes supported by the current platform

Name indicates the name of the NIC device displayed during monitoring.

 

-Net tap [, vlan = n] [, name = name] [, fd = h] [, ifname = name] [, script = file] [, downscript = dfile]

Connect to the specified vlan through the TAP network interface of the physical machine. ifname specifies the name of the second half of the NIC

When a VM is started, the second half of the NIC is not automatically added. You need to add a script using the script = file script (/etc/qemu-ifup by default ).

Configure the current network interface, and use the script specified by downscript = file (default:/etc/qemu-ifdown) to cancel interface configuration, use script = no and

Downscript = no can be used to disable script execution.

 

Qemu-kvm-m 128-cpu host-smp 2-name "first"-drive file =/root/cirros-no_cloud-0.3.0-x86_64-disk.img, if = virtio, media = disk, format = qcow2, cache = writeback-nographic-net nic-net tap, ifname = vnet0.0, script = no

Start a virtual machine without specifying a tap script

 

Return to the physical machine and execute ifconfig-

 

The second half of the VM Nic is automatically added, but it does not take effect because no script is specified.

STARTUP script example: vim/etc/qemu-ifup

    #!/bin/bash    #    bridge=br0    if [ -n "$1" ]; then        ip link set $1 up        sleep 1        brctl addif $bridge $1    [ $? -eq 0 ] && exit 0 || exit 1        else        echo "Error: no interface specified."    exit 1    fi

Qemu-kvm will automatically pass the name of the second half of the VM Nic to this script as a parameter when creating and starting the VM.

 

Example of closing the Script: vim/etc/qemu-ifdown

    #!/bin/bash    #    bridge=br0    if [ -n "$1" ];then        brctl delif $bridge $1        ip link set $1 down        exit 0    else        echo "Error: no interface specified."        exit 1    fi

Qemu-kvm-m 128-cpu host-smp 2-name "first"-drive file =/root/cirros-no_cloud-0.3.0-x86_64-disk.img, if = virtio, media = disk, format = qcow2, cache = writeback-nographic-net nic-net tap, ifname = vnet0.0, script =/etc/qemu-ifup

Verification:

 

Vnet0.0 has been added to the Bridge of the physical machine.

 

Start another virtual machine and configure the network adapter address in the same network segment for the two virtual machines to communicate. However, remember to specify the MAC address

If this parameter is not specified, the MAC addresses of the two VMS are the same, causing communication failure. When configuring MAC, do not change the first three addresses: 52: 54: 00.

For example:

Qemu-kvm-m 128-cpu host-smp 2-name "second"-drive file =/root/cirros-no_cloud-0.3.0-x86_64-disk.img, if = virtio, media = disk, format = qcow2, cache = writeback-nographic-net nic, macaddr = 52: 54: 00: 11: 22: 33-net tap, ifname = vnet0.1, script =/etc/qemu-ifup

The Virtual Network of this isolation model is created.

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.