Open main menu

OpenVZ Virtuozzo Containers Wiki β

Changes

Virtual Ethernet device

10,135 bytes added, 16:39, 2 October 2016
m
no edit summary
<translate><!--T:1-->'''Virtual ethernet Ethernet device ''' is ethernet an Ethernet-like device which that can be used inside a [[VEcontainer]]. Unlikea [[venet ]] network device, a [[veth ]] device has a MAC address. Therefore, it can be used in more configurations. When vethis bridged to a [[CT0]] network interface (e.g., eth0), the container can act as anindependent host on the network. The container's user can set up all of the networkinghimself, including IPs, gateways, etc.
Virtual ethernet <!--T:2-->A virtual Ethernet device consist consists of two ethernet Ethernet devices - ,one in [[VE0CT0]] (e.g., vethN.0) and another a corresponding one in VECT (e. These devices g., eth0) that are connected to each other, so if . If a packet goes is sent to onedevice it will come out from the other device.
== Virtual Ethernet device usage == <!--T:3-->
== Virtual ethernet device usage =Kernel module === <!--T:4-->The <code>vzethdev</code> module should be loaded. You can check it with the following commands.<pre># lsmod | grep vzethvzethdev 8224 0vzmon 35164 5 vzethdev,vznetdev,vzrst,vzcptvzdev 3080 4 vzethdev,vznetdev,vzmon,vzdquota</pre>
=== Adding veth to a VE ===<!--T:5-->In case it is not loaded, load it:
<pre>
vzctl set <VEID> --veth_add <dev_name>,<dev_addr>,<ve_dev_name>,<ve_dev_addr># modprobe vzethdev
</pre>
Here
* <tt>dev_name</tt> is ethernet device name in the [[VE0|host system]]
* <tt>dev_addr</tt> is its MAC address
* <tt>ve_dev_name</tt> is an ethernet device name in the VE
* <tt>ve_dev_addr</tt> is its MAC address
=== MAC addresses === <!--T:6-->The following steps to generate a MAC address are not necessary, since newer versionsof vzctl will automatically generate a MAC address for you. These steps are providedin case you want to set a MAC address manually. <!--T:7-->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. <!--T:8-->MAC addresses must be entered in XX:XX:XX:XX:XX:XX format. Note that  <!--T:9-->There is a utility script available for generating MAC addresses: https://github.com/moutai/eucalyptus-utils/blob/master/easymac.sh. It is used like this option:  <!--T:10-->chmod +x easymac.sh ./easymac.sh -R === Adding veth to a CT === <!--T:11-->  <!--T:12-->vzctl set <CTID> --netif_add <ifname>[,<mac>,<host_ifname>,<host_mac>,<bridge>] <!--T:13-->Here* <tt>ifname</tt> is the Ethernet device name in the CT* <tt>mac</tt> is its MAC address in the CT* <tt>host_ifname</tt> is the Ethernet device name on the host ([[CT0]])* <tt>host_mac</tt> is its MAC address on the host ([[CT0]]), if you want independent communication with the Container through the bridge, you should explicitly specify multicast MAC address here (FE:FF:FF:FF:FF:FF).* <tt>bridge</tt> is incrementalan 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.) <!--T:14-->{{Note|All parameters except <code>ifname</code> are optional. Missing parameters, except for bridge, so devices are added automatically generated, if not specified.}} <!--T:15-->Example:  <!--T:16-->vzctl set 101 --netif_add eth0 --save <!--T:17-->If you want to already existing onesspecify everything:  <!--T:18-->vzctl set 101 --netif_add eth0,00:12:34:56:78:9A,veth101.0,00:12:34:56:78:9B --save <!--T:19-->If you want to use independent communication through the bridge:
==== Examples ==== <pre!--T:20-->vzctl set 101 --veth_add veth101.0netif_add eth0,00:12:34:56:78:9A,eth0veth101.0,00FE:12FF:34FF:56FF:78FF:9B FF,vzbr0 --save </pre!--T:21-->After executing this command If you want to specify the bridge and autogenerate the other values:  <tt!--T:22-->vzctl set 101 --netif_add eth0,,,,vzbr0 --save === Removing vethfrom a CT === </tt!--T:23--> device will be created for VE 101 and veth configuration will be saved to a VE configuration file.Host <!--T:24-side ethernet device will have ->vzctl set <ttCTID>veth101.0--netif_del </ttdev_name> name and |all <tt>00!--T:12:34:56:78:9A</tt25--> MAC address.VE-side ethernet device will have Here* <ttcode>eth0dev_name</ttcode> is the Ethernet device name and in the [[CT]]. <tt>00!--T:12:34:56:78:9B</tt26--> MAC address.{{Note|Use random MAC addresses. Do not use MAC addresses of real eth If you want to remove all Ethernet devicesin CT, beacuse this can lead to collisionsuse <code>all</code>.}}
=== Removing veth from a VE ===<pre>vzctl set <VEID> !--T:27--veth_del <dev_name></pre>Here <tt>dev_name</tt> is the ethernet device name in the [[VE0|host system]].Example:
==== Example ==== <pre!--T:28-->vzctl set 101 --veth_del veth101.0 netif_del eth0 --save</pre>After executing this command veth device with host-side ethernet name veth101.0 will be removed from VE 101 and veth configuration will be updated in VE config file.
== Common configurations with virtual ethernet Ethernet devices ==<!--T:29-->
Module <tt>vzethdev</tt> must be loaded to operate with veth devices.
=== Simple configuration with virtual ethernet Ethernet device ===<!--T:30-->
<!--T:31-->Assuming that 192.168.0.0/24 is being used on your LAN, the following sections show how to configure a container for the LAN using veth. ==== Start a VE CT ====<!--T:32-->  <pre!--T:33-->
[host-node]# vzctl start 101
</pre>
==== Add veth device to VE CT ====<!--T:34-->  <pre!--T:35-->[host-node]# vzctl set 101 --veth_add veth101.0,00:12:34:56:78:9A,netif_add eth0,00:12:34:56:78:9B --save </pre!--T:36-->This allocates a MAC address and associates it with the host eth0 port.
==== Configure devices in VE0 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.
<pre>
[host-node]# ifconfig veth101.0 up0
[host-node]# echo 1 > /proc/sys/net/ipv4/conf/veth101.0/forwarding
[host-node]# echo 1 > /proc/sys/net/ipv4/conf/veth101.0/proxy_arp
</pre>
==== Configure device in VE 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.
<pre>
[host-node]# vzctl enter 101
[ve-101]# /sbin/ifconfig eth0 up0
[ve-101]# /sbin/ip addr add 192.168.0.101 dev eth0
[ve-101]# /sbin/ip route add default dev eth0
</pre>
<!--T:39-->Notes:* Until you ifconfig eth0 it won't appear. When you do it will use the mac address netif_add added earlier* 192.168.0.101 is chosen to be an [[unrouteable private ip address]]. Where 101 reminds you that it is node 101.* The "ip route" tells all traffic to head to "device eth0"* In theory you could [[use dhcpd with OpenVZ]] and dhclient to pick up an DHCP address from your router instead of hardwiring it** http://openvz.org/pipermail/users/2005-November/000020.html ==== Add route in [[VE0CT0]] ==== <!--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 192.168.0.101 dev veth101.0 === Using a directly routed IPv4 with virtual Ethernet device === <!--T:41--> ====Situation ==== <!--T:42-->Hardware Node (HN/CT0) has 192.168.0.1/24 with router 192.168.0.254. <!--T:43-->We also know that IPv4 10.0.0.1/32 is directly routed to 192.168.0.1 (this is called a ''fail-over IP''). <!--T:44-->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 <!--T:49-->This allocates a MAC address and associates it with the host eth0 port. ==== Configure device and add route in CT0 ==== <!--T:50--> <!--T:51--><pre>[host-node]# ifconfig veth101.0 0[host-node]# ip route add 10.0.0.1 dev veth101.0</pre> <!--T:52-->You can automatize this at VPS creation by using a mount script <tt>$VEID.mount</tt>. <!--T:53-->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. <!--T:54-->Contents of the mount script <tt>/etc/vz/conf/101.mount</tt>:
<pre>
#!/bin/bash# This script source VPS configuration files in the same order as vzctl does <!--T:55--># if one of these files does not exist then something is really broken[ -f /etc/vz/vz.conf ] || exit 1[host-nodef $VE_CONFFILE ]|| exit 1 <!--T:56--># ip route add 192source both files. Note the order, it is important. /etc/vz/vz.conf. $VE_CONFFILE <!--T:57--># Configure veth with IP after VPS has started{ IP=X.168Y.0Z.101 dev T DEV=veth101.0 while sleep 1; do /sbin/ifconfig $DEV 0 >/dev/null 2>&1 if [ $? -eq 0 ]; then /sbin/ip route add $IP dev $DEV break fi done} &
</pre>
=== Virtual ethernet device can be used with IPv6 =Make sure IPv4 forwarding is enabled in CT0 ==== <!--T:58-->
==== Start [[VE]] ====<!--T:59-->
<pre>
[host-node]# vzctl start echo 1 > /proc/sys/net/ipv4/ip_forward[host-node]# echo 1 > /proc/sys/net/ipv4/conf/eth0/forwarding[host-node]# echo 1 > /proc/sys/net/ipv4/conf/veth101.0/forwarding</pre>You can permanently set this by using <tt>/etc/sysctl.conf</tt>. ==== Configure device in CT ==== <!--T:60--> <!--T:61-->1. Configure IP address <!--T:62-->2. Add gateway <!--T:63-->3. Add default route <!--T:64--><pre>[ve-101]# /sbin/ifconfig eth0 10.0.0.1 netmask 255.255.255.255[ve-101]# /sbin/ip route add 192.168.0.1 dev eth0[ve-101]# /sbin/ip route add default via 192.168.0.1
</pre>
==== Add veth device to [[VE]] ====<!--T:65-->In a Debian container, you can configure this permanently by using <tt>/etc/network/interfaces</tt>:
<pre>
[host-node]# vzctl set 101 --veth_add veth101auto eth0iface eth0 inet static address 10.0.0.1 netmask 255.255.255.255 up /sbin/ip route add 192.168.0,00:12:34:56:78:9A,.1 dev eth0,00:12:34:56:78:9B --save up /sbin/ip route add default via 192.168.0.1
</pre>
===Virtual Ethernet device with IPv6 = Configure == <!--T:66--> <!--T:67-->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. <!--T:69-->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 in using FE:FF:FF:FF:FF:FF as a [[VE0CT0]] veth side MAC address and then follow these steps. ==== Create bridge device ====<!--T:70-->
<pre>
[host-node]# ifconfig veth101.0 up[host-node]# echo 1 > /proc/sys/net/ipv6/conf/veth101.0/forwarding[host-node]# echo 1 > /proc/sys/net/ipv6/conf/eth0/forwarding[host-node]# echo 1 > /proc/sys/net/ipv6/conf/all/forwardingbrctl addbr vzbr0
</pre>
==== Configure device in [[VE]] Add veth devices to bridge ====<!--T:71-->
<pre>
[host-node]# vzctl enter 101brctl addif vzbr0 veth101.0...[host-node]# brctl addif vzbr0 veth101.n[host-node]# brctl addif vzbr0 veth102.0......[vehost-101node]# /sbin/ifconfig eth0 upbrctl addif vzbr0 vethXXX.N
</pre>
==== Start router advertisement daemon (radvd) for IPv6 in VE0 Configure bridge device ====First you need to edit radvd configuration file. Here is a simple example of <tt>/etc/radv.conf</tt!--T:72-->:
<pre>
interface veth101.[host-node]# ifconfig vzbr0 0{</pre> AdvSendAdvert on; MinRtrAdvInterval 3;=== Automating the bridge === <!--T:73--> MaxRtrAdvInterval 10; AdvHomeAgentFlag off;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]].
prefix 3ffe<!--T:2400:0:0::/6474--> { AdvOnLink In Debian, configure the network interface on; AdvAutonomous on; AdvRouterAddr off; };};[[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.
<!--T:75-->The bridge forwarding delay is set to 0 seconds so that forwarding begins immediately when a new interface eth0is 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.{<pre> AdvSendAdvert on; MinRtrAdvInterval 3; MaxRtrAdvInterval 10; AdvHomeAgentFlag off;iface eth0 inet manual
prefix 3ffe<!--T:0302:0011:0002::/6476-->auto vzbr0iface vzbr0 inet static {bridge_ports eth0 AdvOnLink on; bridge_fd 0 AdvAutonomous on; address 192.168.1.100 AdvRouterAddr off; netmask 255.255.255.0 };};gateway 192.168.1.254
</pre>
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 (sysctl: <code>net.bridge.bridge-nf-call-iptables</code>).
 
=== 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.
 
<!--T:78-->
According to http://bugzilla.openvz.org/show_bug.cgi?id=301 , a bug that stopped the veth device persistent was "Obsoleted now when --veth_add/--veth_del are introduced"
 
<!--T:79-->
See http://wiki.openvz.org/w/index.php?title=Virtual_Ethernet_device&diff=5990&oldid=5989#Making_a_veth-device_persistent for a workaround that used to be described in this section.
 
<!--T:80-->
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-->
Then<!--T:82-->Like the above example, here it is how to add the veth device to a bridge in a persistent way.  <!--T:83-->vzctl includes a 'vznetaddbr' script, start radvdwhich makes use of the ''bridge'' parameter of the --netif_add switch. <!--T:84-->Just create /etc/vz/vznet.conf containing the following. <!--T:85-->
<pre>
[host-node]# EXTERNAL_SCRIPT="/etcusr/init.dsbin/radvd startvznetaddbr"
</pre>
==== Add IPv6 addresses to devices in [[VE0]] ====<!--T:86-->Or just run command
<pre>
[host-node]# ip addr add dev veth101.0 3ffe:2400::212:34ff:fe56:789aecho 'EXTERNAL_SCRIPT="/usr/sbin/vznetaddbr"' > /etc/64[host-node]# ip addr add dev eth0 3ffe:0302:0011:0002:211:22ff:fe33:4455vz/64vznet.conf
</pre>
=== Virtual ethernet devices can be joined in one bridge ===<!--T:87-->Thus youThe script uses 'vmbr0'll have more convinient configuration, i.e. all routes to VEs will bethrough this as default bridge name when no bridge and VEs can communicate with each other even without these routesis specified.
=== Virtual ethernet 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]]* [[Using private IPs for Hardware Nodes]]* Patch: [[Disable venet interface]]* Troubleshooting: [[Bridge doesn't forward packets]] == External links ==<!--T:90-->
* [http://www.tldp.org/HOWTO/Linux+IPv6-HOWTO/hints-daemons-radvd.html Linux IPv6 HOWTO, a chapter about radvd]
* [http://sysadmin-ivanov.blogspot.com/2008/02/2-veth-with-2-bridges-on-openvz-at.html 2 veth with 2 bridges setup]
* [https://forum.proxmox.com/threads/physical-host-with-2-nics-each-with-different-gateways.1733/#post-9287 Non default gateway for CentOS OpenVZ container] - this applies to BlueOnyx in Proxmox as well. | [[Media:TwoGWsPVECentOS.pdf|Cache]]
</translate>
[[Category: Networking]]
[[Category: HOWTO]]
20
edits