freebsd-nq/contrib/hyperv/tools/scripts/hyperv_vfup
Sepherosa Ziehau c685956956 hyperv: Add VF bringup scripts and devd rules.
How network VF works with hn(4) on Hyper-V in non-transparent mode:

- Each network VF has a cooresponding hn(4).
- The network VF and the it's cooresponding hn(4) have the same hardware
  address.
- Once the network VF is up, e.g. ifconfig VF up:
  o  All of the transmission should go through the network VF.
  o  Most of the reception goes through the network VF.
  o  Small amount of reception may go through the cooresponding hn(4).
     This reception will happen, even if the the cooresponding hn(4) is
     down.  The cooresponding hn(4) will change the reception interface
     to the network VF, so that network layer and application layer will
     be tricked into thinking that these packets were received by the
     network VF.
  o  The cooresponding hn(4) pretends the physical link is down.
- Once the network VF is down or detached:
  o  All of the transmission should go through the cooresponding hn(4).
  o  All of the reception goes through the cooresponding hn(4).
  o  The cooresponding hn(4) fallbacks to the original physical link
     detection logic.

All these features are mainly used to help live migration, during which
the network VF will be detached, while the network communication to the
VM must not be cut off.  In order to reach this level of live migration
transparency, we use failover mode lagg(4) with the network VF and the
cooresponding hn(4) attached to it.

To ease user configuration for both network VF and non-network VF, the
lagg(4) will be created by the following rules, and the configuration
of the cooresponding hn(4) will be applied to the lagg(4) automatically.

Sponsored by:	Microsoft
Differential Revision:	https://reviews.freebsd.org/D11635
2017-07-31 07:18:15 +00:00

120 lines
2.0 KiB
Bash

#!/bin/sh
. /etc/rc.subr
. /etc/network.subr
load_rc_config netif
#
# Customized per-interface setup, e.g. hyperv_vfup.hn1
#
# NOTE-CUSTOMIZE:
# Comment this out, if this script is used as template
# for the customized per-interface setup.
#
if [ -f /usr/libexec/hyperv/hyperv_vfup.$1 ]
then
/usr/libexec/hyperv/hyperv_vfup.$1
exit $?
fi
# NOTE-CUSTOMIZE:
#hn=${0##*.}
hn=$1
hn_unit=`echo $hn | sed 's/[^0-9]*//g'`
vf=`sysctl -n dev.hn.$hn_unit.vf`
if [ ! $vf ]
then
# Race happened; VF was removed, before we ran.
echo "$hn: VF was detached"
exit 0
fi
#
# Create laggX for hnX.
# Add VF and hnX to laggX.
#
lagg=lagg$hn_unit
ifconfig $lagg > /dev/null 2>&1
if [ $? -ne 0 ]
then
#
# No laggX, create it now.
#
ifconfig $lagg create > /dev/null 2>&1
if [ $? -ne 0 ]
then
echo "$lagg creation failed"
exit 1
fi
#
# Configure laggX (failover), add hnX and VF to it.
#
ifconfig $lagg laggproto failover laggport $hn laggport $vf
ifconfig $lagg inet6 no_dad
#
# Stop dhclient on hnX, if any.
#
pidfile=/var/run/dhclient.$hn.pid
if [ -f $pidfile ]
then
kill -TERM `cat $pidfile`
fi
#
# Remove all configured IPv4 addresses on hnX, e.g.
# configured by dhclient. laggX will take over the
# network operations.
#
while true
do
ifconfig $hn -alias > /dev/null 2>&1
if [ $? -ne 0 ]
then
break
fi
done
# TODO: Remove IPv6 addresses on hnX
#
# Use hnX's configuration for laggX
#
# NOTE-CUSTOMIZE:
# If this script is used as template for the customized
# per-interface setup, replace this with whatever you
# want to do with the laggX.
#
if dhcpif $hn;
then
ifconfig $lagg up
if syncdhcpif $hn;
then
dhclient $lagg
else
dhclient -b $lagg
fi
else
ifconfig_args=`ifconfig_getargs $hn`
if [ -n "$ifconfig_args" ]
then
ifconfig $lagg $ifconfig_args
fi
fi
else
#
# laggX exists. Check whether VF was there or not.
# If VF was not added to laggX, add it now.
#
ifconfig $lagg | grep "laggport: $vf" > /dev/null 2>&1
if [ $? -ne 0 ]
then
ifconfig $lagg laggport $vf
fi
fi