rdobuilder 167853
#!/bin/bash
rdobuilder 167853
rdobuilder 167853
# Copyright (c) 2011 Alexey I. Froloff.
rdobuilder 167853
#
rdobuilder 167853
# Licensed under the Apache License, Version 2.0 (the "License");
rdobuilder 167853
# you may not use this file except in compliance with the License.
rdobuilder 167853
# You may obtain a copy of the License at:
rdobuilder 167853
#
rdobuilder 167853
#     http://www.apache.org/licenses/LICENSE-2.0
rdobuilder 167853
#
rdobuilder 167853
# Unless required by applicable law or agreed to in writing, software
rdobuilder 167853
# distributed under the License is distributed on an "AS IS" BASIS,
rdobuilder 167853
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rdobuilder 167853
# See the License for the specific language governing permissions and
rdobuilder 167853
# limitations under the License.
rdobuilder 167853
rdobuilder 167853
. /etc/init.d/functions
rdobuilder 167853
rdobuilder 167853
cd /etc/sysconfig/network-scripts
rdobuilder 167853
. ./network-functions
rdobuilder 167853
rdobuilder 167853
[ -f ../network ] && . ../network
rdobuilder 167853
rdobuilder 167853
CONFIG=${1}
rdobuilder 167853
TIMEOUT=10
rdobuilder 167853
rdobuilder 167853
need_config ${CONFIG}
rdobuilder 167853
rdobuilder 167853
source_config
rdobuilder 167853
rdobuilder 167853
OTHERSCRIPT="/etc/sysconfig/network-scripts/ifup-${REAL_DEVICETYPE}"
rdobuilder 167853
rdobuilder 167853
if [ ! -x ${OTHERSCRIPT} ]; then
rdobuilder 167853
        OTHERSCRIPT="/etc/sysconfig/network-scripts/ifup-eth"
rdobuilder 167853
fi
rdobuilder 167853
rdobuilder 167853
check_recursion ()
rdobuilder 167853
{
rdobuilder 167853
        [ -n "${UPPEDSTACK}" ] && for _r in ${UPPEDSTACK}; do
rdobuilder 167853
                [ "$_r" = "$1" ] && return 1
rdobuilder 167853
        done
rdobuilder 167853
rdobuilder 167853
        return 0
rdobuilder 167853
}
rdobuilder 167853
rdobuilder 167853
ifup_ovs_bridge ()
rdobuilder 167853
{
rdobuilder 167853
        if ovs-vsctl br-exists "${OVS_BRIDGE}"; then :; else
rdobuilder 167853
                /sbin/ifup "${OVS_BRIDGE}"
rdobuilder 167853
        fi
rdobuilder 167853
}
rdobuilder 167853
rdobuilder 167853
if [ -z "${UPPEDSTACK}" ]; then
rdobuilder 167853
        UPPEDSTACK="${DEVICE}"
rdobuilder 167853
fi
rdobuilder 167853
rdobuilder 167853
[ -n "${OVSREQUIRES}" ] && for _i in ${OVSREQUIRES}; do
rdobuilder 167853
        if ( check_recursion "$_i" ); then
rdobuilder 167853
                UPPEDSTACK="${UPPEDSTACK} $_i" /sbin/ifup "$_i"
rdobuilder 167853
        fi
rdobuilder 167853
done
rdobuilder 167853
rdobuilder 167853
SERVICE_UNIT=/usr/lib/systemd/system/openvswitch.service
rdobuilder 167853
if [ -f $SERVICE_UNIT ] && [ -x /usr/bin/systemctl ]; then
rdobuilder 167853
        if ! systemctl --quiet is-active openvswitch.service; then
rdobuilder 167853
                systemctl start openvswitch.service
rdobuilder 167853
        fi
rdobuilder 167853
else
rdobuilder 167853
        if [ ! -f /var/lock/subsys/openvswitch ]; then
rdobuilder 167853
                /sbin/service openvswitch start
rdobuilder 167853
        fi
rdobuilder 167853
fi
rdobuilder 167853
rdobuilder 167853
case "$TYPE" in
rdobuilder 167853
        OVSBridge|OVSUserBridge)
rdobuilder 167853
                # If bridge already exists and is up, it has been configured through
rdobuilder 167853
                # other cases like OVSPort, OVSIntPort and OVSBond. If it is down or
rdobuilder 167853
                # it does not exist, create it. It is possible for a bridge to exist
rdobuilder 167853
                # because it remained in the OVSDB for some reason, but it won't be up.
rdobuilder 167853
                if [ "${TYPE}" = "OVSUserBridge" ]; then
rdobuilder 167853
                        DATAPATH="netdev"
rdobuilder 167853
                fi
rdobuilder 167853
                if check_device_down "${DEVICE}"; then
rdobuilder 167853
                        ovs-vsctl -t ${TIMEOUT} -- --may-exist add-br "$DEVICE" $OVS_OPTIONS \
rdobuilder 167853
                        ${OVS_EXTRA+-- $OVS_EXTRA} \
rdobuilder 167853
                        ${STP+-- set bridge "$DEVICE" stp_enable="${STP}"} \
rdobuilder 167853
                        ${DATAPATH+-- set bridge "$DEVICE" datapath_type="$DATAPATH"}
rdobuilder 167853
                else
rdobuilder 167853
                        OVSBRIDGECONFIGURED="yes"
rdobuilder 167853
                fi
rdobuilder 167853
rdobuilder 167853
                # If MACADDR is provided in the interface configuration file,
rdobuilder 167853
                # we need to set it using ovs-vsctl; setting it with the "ip"
rdobuilder 167853
                # command in ifup-eth does not make the change persistent.
rdobuilder 167853
                if [ -n "$MACADDR" ]; then
rdobuilder 167853
                        ovs-vsctl -t ${TIMEOUT} -- set bridge "$DEVICE" \
rdobuilder 167853
                                other-config:hwaddr="$MACADDR"
rdobuilder 167853
                fi
rdobuilder 167853
rdobuilder 167853
                # When dhcp is enabled, the assumption is that there will be a port to
rdobuilder 167853
                # attach (otherwise, we can't reach out for dhcp). So, we do not
rdobuilder 167853
                # configure the bridge through rhel's ifup infrastructure unless
rdobuilder 167853
                # it is being configured after the port has been configured.
rdobuilder 167853
                # The "OVSINTF" is set only after the port is configured.
rdobuilder 167853
                if [ "${OVSBOOTPROTO}" = "dhcp" ] && [ -n "${OVSINTF}" ]; then
rdobuilder 167853
                        case " ${OVSDHCPINTERFACES} " in
rdobuilder 167853
                                *" ${OVSINTF} "*)
rdobuilder 167853
                                        BOOTPROTO=dhcp ${OTHERSCRIPT} ${CONFIG}
rdobuilder 167853
                                ;;
rdobuilder 167853
                        esac
rdobuilder 167853
                fi
rdobuilder 167853
rdobuilder 167853
                # When dhcp is not enabled, it is possible that someone may want
rdobuilder 167853
                # a standalone bridge (i.e it may not have any ports). Configure it.
rdobuilder 167853
                if [ "${OVSBOOTPROTO}" != "dhcp" ] && [ -z "${OVSINTF}" ] && \
rdobuilder 167853
                        [ "${OVSBRIDGECONFIGURED}" != "yes" ]; then
rdobuilder 167853
                        ${OTHERSCRIPT} ${CONFIG}
rdobuilder 167853
                fi
rdobuilder 167853
                exit 0
rdobuilder 167853
                ;;
rdobuilder 167853
        OVSPort)
rdobuilder 167853
                ifup_ovs_bridge
rdobuilder 167853
                ${OTHERSCRIPT} ${CONFIG} ${2}
rdobuilder 167853
                # The port might be already in the database but not yet
rdobuilder 167853
                # in the datapath.  So, remove the stale interface first.
rdobuilder 167853
                ovs-vsctl -t ${TIMEOUT} \
rdobuilder 167853
                        -- --if-exists del-port "$OVS_BRIDGE" "$DEVICE" \
rdobuilder 167853
                        -- add-port "$OVS_BRIDGE" "$DEVICE" $OVS_OPTIONS ${OVS_EXTRA+-- $OVS_EXTRA}
rdobuilder 167853
                OVSINTF=${DEVICE} /sbin/ifup "$OVS_BRIDGE"
rdobuilder 167853
                ;;
rdobuilder 167853
        OVSIntPort)
rdobuilder 167853
                ifup_ovs_bridge
rdobuilder 167853
                ovs-vsctl -t ${TIMEOUT} \
rdobuilder 167853
                        -- --if-exists del-port "$OVS_BRIDGE" "$DEVICE" \
rdobuilder 167853
                        -- add-port "$OVS_BRIDGE" "$DEVICE" $OVS_OPTIONS \
rdobuilder 167853
                        -- set Interface "$DEVICE" type=internal ${OVS_EXTRA+-- $OVS_EXTRA}
rdobuilder 167853
                if [ -n "${OVSDHCPINTERFACES}" ]; then
rdobuilder 167853
                        for _iface in ${OVSDHCPINTERFACES}; do
rdobuilder 167853
                                /sbin/ifup ${_iface}
rdobuilder 167853
                        done
rdobuilder 167853
                fi
rdobuilder 167853
                BOOTPROTO="${OVSBOOTPROTO}" ${OTHERSCRIPT} ${CONFIG} ${2}
rdobuilder 167853
                ;;
rdobuilder 167853
        OVSBond)
rdobuilder 167853
                ifup_ovs_bridge
rdobuilder 167853
                for _iface in $BOND_IFACES; do
rdobuilder 167853
                        /sbin/ifup ${_iface}
rdobuilder 167853
                done
rdobuilder 167853
                ovs-vsctl -t ${TIMEOUT} \
rdobuilder 167853
                        -- --if-exists del-port "$OVS_BRIDGE" "$DEVICE" \
rdobuilder 167853
                        -- add-bond "$OVS_BRIDGE" "$DEVICE" ${BOND_IFACES} $OVS_OPTIONS ${OVS_EXTRA+-- $OVS_EXTRA}
rdobuilder 167853
                OVSINTF=${DEVICE} /sbin/ifup "$OVS_BRIDGE"
rdobuilder 167853
                ;;
rdobuilder 167853
        OVSTunnel)
rdobuilder 167853
                ifup_ovs_bridge
rdobuilder 167853
                ovs-vsctl -t ${TIMEOUT} \
rdobuilder 167853
                        -- --if-exists del-port "$OVS_BRIDGE" "$DEVICE" \
rdobuilder 167853
                        -- add-port "$OVS_BRIDGE" "$DEVICE" $OVS_OPTIONS \
rdobuilder 167853
                        -- set Interface "$DEVICE" type=$OVS_TUNNEL_TYPE $OVS_TUNNEL_OPTIONS ${OVS_EXTRA+-- $OVS_EXTRA}
rdobuilder 167853
                ;;
rdobuilder 167853
        OVSPatchPort)
rdobuilder 167853
                ifup_ovs_bridge
rdobuilder 167853
                ovs-vsctl -t ${TIMEOUT} \
rdobuilder 167853
                        -- --if-exists del-port "$OVS_BRIDGE" "$DEVICE" \
rdobuilder 167853
                        -- add-port "$OVS_BRIDGE" "$DEVICE" $OVS_OPTIONS \
rdobuilder 167853
                        -- set Interface "$DEVICE" type=patch options:peer="${OVS_PATCH_PEER}" ${OVS_EXTRA+-- $OVS_EXTRA}
rdobuilder 167853
                ;;
rdobuilder 167853
        OVSDPDKPort)
rdobuilder 167853
                ifup_ovs_bridge
rdobuilder 167853
                BRIDGE_MAC_ORIG=$(get_hwaddr $OVS_BRIDGE)
rdobuilder 167853
                ovs-vsctl -t ${TIMEOUT} \
rdobuilder 167853
                        -- --if-exists del-port "$OVS_BRIDGE" "$DEVICE" \
rdobuilder 167853
                        -- add-port "$OVS_BRIDGE" "$DEVICE" $OVS_OPTIONS \
rdobuilder 167853
                        -- set Interface "$DEVICE" type=dpdk ${OVS_EXTRA+-- $OVS_EXTRA}
rdobuilder 167853
                BRIDGE_MAC=$(get_hwaddr $OVS_BRIDGE)
rdobuilder 167853
                # The bridge may change its MAC to be the lower one among all its
rdobuilder 167853
                # ports. If that happens, bridge configuration (e.g. routes) will
rdobuilder 167853
                # be lost. Restore the post-up bridge configuration again.
rdobuilder 167853
                if [ "$BRIDGE_MAC_ORIG" != "$BRIDGE_MAC" ]; then
rdobuilder 167853
                        ${OTHERSCRIPT} "$OVS_BRIDGE"
rdobuilder 167853
                fi
rdobuilder 167853
                ;;
rdobuilder 167853
        OVSDPDKVhostUserPort)
rdobuilder 167853
                ifup_ovs_bridge
rdobuilder 167853
                PORT_TYPE="dpdkvhostuser"
rdobuilder 167853
                PORT_PATH=""
rdobuilder 167853
                if [ "$OVS_PORT_MODE" == "client" ]; then
rdobuilder 167853
                        PORT_TYPE="dpdkvhostuserclient"
rdobuilder 167853
                        PORT_PATH="options:vhost-server-path=${OVS_PORT_PATH}"
rdobuilder 167853
                fi
rdobuilder 167853
                ovs-vsctl -t ${TIMEOUT} \
rdobuilder 167853
                        -- --if-exists del-port "$OVS_BRIDGE" "$DEVICE" \
rdobuilder 167853
                        -- add-port "$OVS_BRIDGE" "$DEVICE" $OVS_OPTIONS \
rdobuilder 167853
                        -- set Interface "$DEVICE" type=$PORT_TYPE \
rdobuilder 167853
                        $PORT_PATH \
rdobuilder 167853
                        ${OVS_EXTRA+-- $OVS_EXTRA}
rdobuilder 167853
                ;;
rdobuilder 167853
        OVSDPDKBond)
rdobuilder 167853
                ifup_ovs_bridge
rdobuilder 167853
                BRIDGE_MAC_ORIG=$(get_hwaddr $OVS_BRIDGE)
rdobuilder 167853
                for _iface in $BOND_IFACES; do
rdobuilder 167853
                        IFACE_TYPES="${IFACE_TYPES} -- set interface ${_iface} type=dpdk"
rdobuilder 167853
                done
rdobuilder 167853
                ovs-vsctl -t ${TIMEOUT} \
rdobuilder 167853
                        -- --if-exists del-port "$OVS_BRIDGE" "$DEVICE" \
rdobuilder 167853
                        -- add-bond "$OVS_BRIDGE" "$DEVICE" ${BOND_IFACES} $OVS_OPTIONS ${IFACE_TYPES} ${OVS_EXTRA+-- $OVS_EXTRA}
rdobuilder 167853
                BRIDGE_MAC=$(get_hwaddr $OVS_BRIDGE)
rdobuilder 167853
                # The bridge may change its MAC to be the lower one among all its
rdobuilder 167853
                # ports. If that happens, bridge configuration (e.g. routes) will
rdobuilder 167853
                # be lost. Restore the post-up bridge configuration again.
rdobuilder 167853
                if [ "$BRIDGE_MAC_ORIG" != "$BRIDGE_MAC" ]; then
rdobuilder 167853
                        ${OTHERSCRIPT} "$OVS_BRIDGE"
rdobuilder 167853
                fi
rdobuilder 167853
                ;;
rdobuilder 167853
        *)
rdobuilder 167853
                echo $"Invalid OVS interface type $TYPE"
rdobuilder 167853
                exit 1
rdobuilder 167853
                ;;
rdobuilder 167853
esac