Jump to: navigation, search

Virtual Ethernet device

1,071 bytes added, 10:39, 31 December 2015
Marked this version for translation
'''Virtual Ethernet device''' is an Ethernet-like device that can be used
inside a [[container]]. Unlike a [[venet]] network device, a [[veth]] device
himself, including IPs, gateways, etc.
A virtual Ethernet device consists of two Ethernet devices,
one in [[CT0]] (e.g., vethN.0) and a corresponding one in CT (e.g., eth0) that are
connected to each other. If a packet is sent to one device it will come out the other device.
== Virtual Ethernet device usage ==<!--T:3-->
=== Kernel module ===<!--T:4-->
The <code>vzethdev</code> module should be loaded. You can check it with the following commands.
In case it is not loaded, load it:
=== MAC addresses ===<!--T:6-->
The following steps to generate a MAC address are not necessary, since newer versions
of vzctl will automatically generate a MAC address for you. These steps are provided
in case you want to set a MAC address manually.
You should use a random MAC address when adding a network interface to a container. Do not use MAC addresses of real eth devices, because this can lead to collisions.
MAC addresses must be entered in XX:XX:XX:XX:XX:XX format.
There is a utility script available for generating MAC addresses: It is used like this:
<!--T:10-->chmod +x
./ -R
=== Adding veth to a CT ===<!--T:11-->
<!--T:12-->vzctl set <CTID> --netif_add <ifname>[,<mac>,<host_ifname>,<host_mac>,<bridge>]
* <tt>ifname</tt> is the Ethernet device name in the CT
* <tt>bridge</tt> is an optional parameter which can be used in custom network start scripts to automatically add the interface to a bridge. (See the reference to the vznetaddbr script below and persistent bridge configurations.)
{{Note|All parameters except <code>ifname</code> are optional. Missing parameters, except for bridge, are automatically generated, if not specified.}}
<!--T:16-->vzctl set 101 --netif_add eth0 --save
If you want to specify everything:
<!--T:18-->vzctl set 101 --netif_add eth0,00:12:34:56:78:9A,veth101.0,00:12:34:56:78:9B --save
If you want to use independent communication through the bridge:
<!--T:20-->vzctl set 101 --netif_add eth0,00:12:34:56:78:9A,veth101.0,FE:FF:FF:FF:FF:FF,vzbr0 --save
If you want to specify the bridge and autogenerate the other values:
<!--T:22-->vzctl set 101 --netif_add eth0,,,,vzbr0 --save
=== Removing veth from a CT ===<!--T:23-->
<!--T:24-->vzctl set <CTID> --netif_del <dev_name>|all
* <code>dev_name</code> is the Ethernet device name in the [[CT]].
{{Note|If you want to remove all Ethernet devices in CT, use <code>all</code>.}}
<!--T:28-->vzctl set 101 --netif_del eth0 --save
== Common configurations with virtual Ethernet devices ==<!--T:29-->
Module <tt>vzethdev</tt> must be loaded to operate with veth devices.
=== Simple configuration with virtual Ethernet device ===<!--T:30-->
Assuming that is being used on your LAN, the following sections show how to configure a container for the LAN using veth.
==== Start a CT ====<!--T:32-->
<!--T:33-->[host-node]# vzctl start 101
==== Add veth device to CT ====<!--T:34-->
<!--T:35-->[host-node]# vzctl set 101 --netif_add eth0 --save
This allocates a MAC address and associates it with the host eth0 port.
==== Configure devices in CT0 ====<!--T:37-->
The following steps are needed when the [[CT]] is '''not''' bridged to a [[CT0]] network interface. That is because the [[CT]] is connected to a virtual network that is "behind" [[CT0]]. [[CT0]] must forward packets between its physical network interface and the virtual network interface where [[CT]] is located. The first step below to configure the interface is not necessary if the container has been started, since the device will have been initialized.
==== Configure device in CT ====<!--T:38-->
The following steps show an example of a quick manual configuration of the [[CT]] network interface. Typically, you would configure the network settings in /etc/network/interfaces (Debian, see below) or however it is normally configured on your distribution. You can also comment or remove the configuration for venet0, if it exists, because that device will not be used.
* Until you ifconfig eth0 it won't appear. When you do it will use the mac address netif_add added earlier
==== Add route in [[CT0]] ====<!--T:40-->
Since [[CT0]] is acting as a router between its physical network interface and the virtual network interface of the [[CT]], we need to add a route to the [[CT]] to direct traffic to the right destination.
[host-node]# ip route add dev veth101.0
=== Using a directly routed IPv4 with virtual Ethernet device ===<!--T:41-->
==== Situation ====<!--T:42-->
Hardware Node (HN/CT0) has with router
We also know that IPv4 is directly routed to (this is called a ''fail-over IP'').
We want to give this directly routed IPv4 address to a container (CT).
==== Start container ====<!--T:45-->
<!--T:46-->[host-node]# vzctl start 101
==== Add veth device to CT ====<!--T:47-->
<!--T:48-->[host-node]# vzctl set 101 --netif_add eth0 --save
This allocates a MAC address and associates it with the host eth0 port.
==== Configure device and add route in CT0 ====<!--T:50-->
[host-node]# ifconfig veth101.0 0
You can automatize this at VPS creation by using a mount script <tt>$VEID.mount</tt>.
The problem here is that the ''veth'' interface appears in CT0 '''after''' VPS has started, therefore we cannot directly use the commands in the mount script. We launch a shell script (enclosed by { }) in background (operator '''&''') that waits for the interface to be ready and then adds the IP route.
Contents of the mount script <tt>/etc/vz/conf/101.mount</tt>:
# This script source VPS configuration files in the same order as vzctl does
# if one of these files does not exist then something is really broken
[ -f /etc/vz/vz.conf ] || exit 1
[ -f $VE_CONFFILE ] || exit 1
# source both files. Note the order, it is important
. /etc/vz/vz.conf
# Configure veth with IP after VPS has started
==== Make sure IPv4 forwarding is enabled in CT0 ====<!--T:58-->
[host-node]# echo 1 > /proc/sys/net/ipv4/ip_forward
You can permanently set this by using <tt>/etc/sysctl.conf</tt>.
==== Configure device in CT ====<!--T:60-->
1. Configure IP address
2. Add gateway
3. Add default route
[ve-101]# /sbin/ifconfig eth0 netmask
In a Debian container, you can configure this permanently by using <tt>/etc/network/interfaces</tt>:
=== Virtual Ethernet device with IPv6 ===<!--T:66-->
See the [[VEs and HNs in same subnets]] article.
=== Independent Virtual Ethernet communication through the bridge ===<!--T:68-->
Bridging a [[CT]] interface to a [[CT0]] interface is the magic that allows the [[CT]] to be an independent host on the network with its own IP address, gateway, etc. [[CT0]] does not need any configuration for forwarding packets to the [[CT]] or performing proxy arp for the [[CT]] or event the routing.
To manually configure a bridge and add devices to it, perform steps 1 - 4 from Simple configuration chapter for several containers and/or veth devices using FE:FF:FF:FF:FF:FF as a [[CT0]] veth side MAC address and then follow these steps.
==== Create bridge device ====<!--T:70-->
[host-node]# brctl addbr vzbr0
==== Add veth devices to bridge ====<!--T:71-->
[host-node]# brctl addif vzbr0 veth101.0
==== Configure bridge device ====<!--T:72-->
[host-node]# ifconfig vzbr0 0
=== Automating the bridge ===<!--T:73-->
The most convenient method is to automatically create the bridge at boot as a network interface, add the physical interface from [[CT0]] and then add the interface from each [[CT]] as it starts. All devices are connected to a virtual switch, and containers directly access the network just as any other host without additional configuration on [[CT0]].
In Debian, configure the network interface on [[CT0]] to plug into a bridge in /etc/network/interfaces. The [[CT0]] physical device is added to the bridge as the "uplink" port to the physical network. You need to have bridge-utils installed for this to work.
The bridge forwarding delay is set to 0 seconds so that forwarding begins immediately when a new interface is added to a bridge. The default delay is 30 seconds, during which the bridge pauses all traffic to listen and figure out where devices are. This can interrupt services when a container is added to the bridge. If you aren't running the spanning tree protocol (off by default) and the bridge does not create a loop in your network, then there is no need for a forwarding delay.
iface eth0 inet manual
auto vzbr0
iface vzbr0 inet static
Follow the steps below for making a veth bridge persistent with the included script. That will automatically add each container to the bridge when it is started. Finally, specify vzbr0 as the bridge when adding the network interface to a container, as describe above. No configuration is needed on [[CT0]] for forwarding packets, proxy arp or additional routes. The interface in each [[CT]] can be configured as desired. Everything "just works" according to normal network interface configuration and default routing rules. Note that as discussed in the troubleshooting section below, bridged packets by default pass through the FORWARD iptables chain. Take care when adding rules to that table that bridged packets are not mistakenly blocked. This behavior can be disabled, if desired.
=== Making a veth-device persistent ===<!--T:77-->
These steps are no longer necessary, as the veth device is automatically created when the container is started. They remain here as a reference.
According to , a bug that stopped the veth device persistent was "Obsoleted now when --veth_add/--veth_del are introduced"
See for a workaround that used to be described in this section.
That's it! At this point, when you restart the CT you should see a new line in the output, indicating that the interface is being configured and a new route being added. And you should be able to ping the host, and to enter the CT and use the network.
=== Making a bridged veth-device persistent ===<!--T:81-->
Like the above example, here it is how to add the veth device to a bridge in a persistent way.
vzctl includes a 'vznetaddbr' script, which makes use of the ''bridge'' parameter of the --netif_add switch.
Just create /etc/vz/vznet.conf containing the following.
Or just run command
The script uses 'vmbr0' as default bridge name when no bridge is specified.
=== Virtual Ethernet devices + VLAN ===<!--T:88-->
This configuration can be done by adding vlan device to the previous configuration.
== See also ==<!--T:89-->
* [[Virtual network device]]
* [[Differences between venet and veth]]
* Troubleshooting: [[Bridge doesn't forward packets]]
== External links ==<!--T:90-->
* [ Linux IPv6 HOWTO, a chapter about radvd]
* [ 2 veth with 2 bridges setup]

Navigation menu