xref: /linux/Documentation/networking/net_failover.rst (revision e3966940559d52aa1800a008dcfeec218dd31f88)
1.. SPDX-License-Identifier: GPL-2.0
2
3============
4NET_FAILOVER
5============
6
7Overview
8========
9
10The net_failover driver provides an automated failover mechanism via APIs
11to create and destroy a failover master netdev and manages a primary and
12standby slave netdevs that get registered via the generic failover
13infrastructure.
14
15The failover netdev acts a master device and controls 2 slave devices. The
16original paravirtual interface is registered as 'standby' slave netdev and
17a passthru/vf device with the same MAC gets registered as 'primary' slave
18netdev. Both 'standby' and 'failover' netdevs are associated with the same
19'pci' device. The user accesses the network interface via 'failover' netdev.
20The 'failover' netdev chooses 'primary' netdev as default for transmits when
21it is available with link up and running.
22
23This can be used by paravirtual drivers to enable an alternate low latency
24datapath. It also enables hypervisor controlled live migration of a VM with
25direct attached VF by failing over to the paravirtual datapath when the VF
26is unplugged.
27
28virtio-net accelerated datapath: STANDBY mode
29=============================================
30
31net_failover enables hypervisor controlled accelerated datapath to virtio-net
32enabled VMs in a transparent manner with no/minimal guest userspace changes.
33
34To support this, the hypervisor needs to enable VIRTIO_NET_F_STANDBY
35feature on the virtio-net interface and assign the same MAC address to both
36virtio-net and VF interfaces.
37
38Here is an example libvirt XML snippet that shows such configuration:
39::
40
41  <interface type='network'>
42    <mac address='52:54:00:00:12:53'/>
43    <source network='enp66s0f0_br'/>
44    <target dev='tap01'/>
45    <model type='virtio'/>
46    <driver name='vhost' queues='4'/>
47    <link state='down'/>
48    <teaming type='persistent'/>
49    <alias name='ua-backup0'/>
50  </interface>
51  <interface type='hostdev' managed='yes'>
52    <mac address='52:54:00:00:12:53'/>
53    <source>
54      <address type='pci' domain='0x0000' bus='0x42' slot='0x02' function='0x5'/>
55    </source>
56    <teaming type='transient' persistent='ua-backup0'/>
57  </interface>
58
59In this configuration, the first device definition is for the virtio-net
60interface and this acts as the 'persistent' device indicating that this
61interface will always be plugged in. This is specified by the 'teaming' tag with
62required attribute type having value 'persistent'. The link state for the
63virtio-net device is set to 'down' to ensure that the 'failover' netdev prefers
64the VF passthrough device for normal communication. The virtio-net device will
65be brought UP during live migration to allow uninterrupted communication.
66
67The second device definition is for the VF passthrough interface. Here the
68'teaming' tag is provided with type 'transient' indicating that this device may
69periodically be unplugged. A second attribute - 'persistent' is provided and
70points to the alias name declared for the virtio-net device.
71
72Booting a VM with the above configuration will result in the following 3
73interfaces created in the VM:
74::
75
76  4: ens10: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
77      link/ether 52:54:00:00:12:53 brd ff:ff:ff:ff:ff:ff
78      inet 192.168.12.53/24 brd 192.168.12.255 scope global dynamic ens10
79         valid_lft 42482sec preferred_lft 42482sec
80      inet6 fe80::97d8:db2:8c10:b6d6/64 scope link
81         valid_lft forever preferred_lft forever
82  5: ens10nsby: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master ens10 state DOWN group default qlen 1000
83      link/ether 52:54:00:00:12:53 brd ff:ff:ff:ff:ff:ff
84  7: ens11: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq master ens10 state UP group default qlen 1000
85      link/ether 52:54:00:00:12:53 brd ff:ff:ff:ff:ff:ff
86
87Here, ens10 is the 'failover' master interface, ens10nsby is the slave 'standby'
88virtio-net interface, and ens11 is the slave 'primary' VF passthrough interface.
89
90One point to note here is that some user space network configuration daemons
91like systemd-networkd, ifupdown, etc, do not understand the 'net_failover'
92device; and on the first boot, the VM might end up with both 'failover' device
93and VF acquiring IP addresses (either same or different) from the DHCP server.
94This will result in lack of connectivity to the VM. So some tweaks might be
95needed to these network configuration daemons to make sure that an IP is
96received only on the 'failover' device.
97
98Below is the patch snippet used with 'cloud-ifupdown-helper' script found on
99Debian cloud images::
100
101  @@ -27,6 +27,8 @@ do_setup() {
102       local working="$cfgdir/.$INTERFACE"
103       local final="$cfgdir/$INTERFACE"
104
105  +    if [ -d "/sys/class/net/${INTERFACE}/master" ]; then exit 0; fi
106  +
107       if ifup --no-act "$INTERFACE" > /dev/null 2>&1; then
108           # interface is already known to ifupdown, no need to generate cfg
109           log "Skipping configuration generation for $INTERFACE"
110
111
112Live Migration of a VM with SR-IOV VF & virtio-net in STANDBY mode
113==================================================================
114
115net_failover also enables hypervisor controlled live migration to be supported
116with VMs that have direct attached SR-IOV VF devices by automatic failover to
117the paravirtual datapath when the VF is unplugged.
118
119Here is a sample script that shows the steps to initiate live migration from
120the source hypervisor. Note: It is assumed that the VM is connected to a
121software bridge 'br0' which has a single VF attached to it along with the vnet
122device to the VM. This is not the VF that was passthrough'd to the VM (seen in
123the vf.xml file).
124::
125
126  # cat vf.xml
127  <interface type='hostdev' managed='yes'>
128    <mac address='52:54:00:00:12:53'/>
129    <source>
130      <address type='pci' domain='0x0000' bus='0x42' slot='0x02' function='0x5'/>
131    </source>
132    <teaming type='transient' persistent='ua-backup0'/>
133  </interface>
134
135  # Source Hypervisor migrate.sh
136  #!/bin/bash
137
138  DOMAIN=vm-01
139  PF=ens6np0
140  VF=ens6v1             # VF attached to the bridge.
141  VF_NUM=1
142  TAP_IF=vmtap01        # virtio-net interface in the VM.
143  VF_XML=vf.xml
144
145  MAC=52:54:00:00:12:53
146  ZERO_MAC=00:00:00:00:00:00
147
148  # Set the virtio-net interface up.
149  virsh domif-setlink $DOMAIN $TAP_IF up
150
151  # Remove the VF that was passthrough'd to the VM.
152  virsh detach-device --live --config $DOMAIN $VF_XML
153
154  ip link set $PF vf $VF_NUM mac $ZERO_MAC
155
156  # Add FDB entry for traffic to continue going to the VM via
157  # the VF -> br0 -> vnet interface path.
158  bridge fdb add $MAC dev $VF
159  bridge fdb add $MAC dev $TAP_IF master
160
161  # Migrate the VM
162  virsh migrate --live --persistent $DOMAIN qemu+ssh://$REMOTE_HOST/system
163
164  # Clean up FDB entries after migration completes.
165  bridge fdb del $MAC dev $VF
166  bridge fdb del $MAC dev $TAP_IF master
167
168On the destination hypervisor, a shared bridge 'br0' is created before migration
169starts, and a VF from the destination PF is added to the bridge. Similarly an
170appropriate FDB entry is added.
171
172The following script is executed on the destination hypervisor once migration
173completes, and it reattaches the VF to the VM and brings down the virtio-net
174interface::
175
176  # reattach-vf.sh
177  #!/bin/bash
178
179  bridge fdb del 52:54:00:00:12:53 dev ens36v0
180  bridge fdb del 52:54:00:00:12:53 dev vmtap01 master
181  virsh attach-device --config --live vm01 vf.xml
182  virsh domif-setlink vm01 vmtap01 down
183