Merge pull request #61 from BarbarossaTM/link

Refactor management of vlans and dummy interfaces
This commit is contained in:
Maximilian Wilhelm 2020-09-05 00:29:31 +02:00 committed by GitHub
commit 9784392a68
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 38 deletions

View file

@ -4,10 +4,6 @@ set -e
[ -n "$VERBOSE" ] && set -x [ -n "$VERBOSE" ] && set -x
IF_LINK_OPTIONS="$IF_LINK_OPTIONS"
[ -n "$IF_MTU" ] && IF_LINK_OPTIONS="$IF_LINK_OPTIONS mtu $IF_MTU"
[ -n "$IF_HWADDRESS" ] && IF_LINK_OPTIONS="$IF_LINK_OPTIONS address $IF_HWADDRESS"
is_vlan() { is_vlan() {
case "$IFACE" in case "$IFACE" in
*#*) return 1 ;; *#*) return 1 ;;
@ -36,26 +32,23 @@ pre-up)
if [ "${IF_LINK_TYPE}" = "dummy" ]; then if [ "${IF_LINK_TYPE}" = "dummy" ]; then
if [ -d "/sys/class/net/${IFACE}" ]; then if [ -d "/sys/class/net/${IFACE}" ]; then
iface_type=$(ip -d link show dev "${IFACE}" | head -n3 | tail -n1 | awk '{ print $1 }') iface_type=$(ip -d link show dev "${IFACE}" | head -n3 | tail -n1 | awk '{ print $1 }')
if [ "${iface_type}" = 'dummy' ]; then if [ "${iface_type}" != 'dummy' ]; then
exit 0 echo "Interface ${IFACE} exists but is of type ${iface_type} instead of dummy"
exit 1
fi fi
fi fi
ip link add "${IFACE}" type dummy ${MOCK} ip link add "${IFACE}" type dummy
fi fi
;;
up|down)
if is_vlan; then
ADD_DEL="add"
[ "$PHASE" = "down" ] && ADD_DEL="delete"
if [ "$PHASE" = "up" -a -e /sys/class/net/$IFACE ]; then if is_vlan; then
if [ -d "/sys/class/net/${IFACE}" ]; then
exit 0 exit 0
fi fi
if [ -z "$MOCK" ]; then if [ -z "${MOCK}" ]; then
if ! ip link show "$IF_VLAN_RAW_DEVICE" >/dev/null; then if [ ! -d "/sys/class/net/${IF_VLAN_RAW_DEVICE}" ]; then
echo "Device $IF_VLAN_RAW_DEVICE for $IFACE does not exist" echo "Underlay device ${IF_VLAN_RAW_DEVICE} for ${IFACE} does not exist"
exit 1 exit 1
fi fi
@ -65,22 +58,31 @@ up|down)
fi fi
fi fi
${MOCK} ip link $ADD_DEL link "$IF_VLAN_RAW_DEVICE" name "$IFACE" type vlan id "$IF_VLAN_ID" ${MOCK} ip link add link "${IF_VLAN_RAW_DEVICE}" name "${IFACE}" type vlan id "${IF_VLAN_ID}"
[ "$PHASE" = "down" ] && exit 0
fi fi
;;
up)
IF_LINK_OPTIONS="$IF_LINK_OPTIONS"
[ -n "$IF_MTU" ] && IF_LINK_OPTIONS="$IF_LINK_OPTIONS mtu $IF_MTU"
[ -n "$IF_HWADDRESS" ] && IF_LINK_OPTIONS="$IF_LINK_OPTIONS address $IF_HWADDRESS"
${MOCK} ip link set $PHASE dev $IFACE $IF_LINK_OPTIONS ${MOCK} ip link set up dev "${IFACE}" ${IF_LINK_OPTIONS}
# Set alias is configured # Set alias is configured
if [ "${IF_ALIAS}" ]; then if [ "${IF_ALIAS}" ]; then
${MOCK} ip link set alias "${IF_ALIAS}" dev "${IFACE}" ${MOCK} ip link set alias "${IF_ALIAS}" dev "${IFACE}"
fi fi
;; ;;
down)
${MOCK} ip link set down dev "${IFACE}"
;;
post-down) post-down)
if [ "${IF_LINK_TYPE}" = "dummy" ]; then if [ "${IF_LINK_TYPE}" = "dummy" ] || is_vlan; then
if [ -d "/sys/class/net/${IFACE}" ]; then if [ -z "${MOCK}" -a ! -d "/sys/class/net/${IFACE}" ]; then
ip link del "${IFACE}" exit 0
fi fi
${MOCK} ip link del "${IFACE}"
fi fi
;; ;;
depend) depend)

View file

@ -8,12 +8,13 @@ tests_init \
down \ down \
mtu \ mtu \
hwaddress \ hwaddress \
vlan_explicit_up \ vlan_explicit_pre_up \
vlan_explicit_down \ vlan_explicit_post_down \
vlan_guessed_up \ vlan_guessed_pre_up \
vlan_guessed_down \ vlan_guessed_post_down \
vlan_explicit_depend \ vlan_explicit_depend \
vlan_guessed_depend vlan_guessed_depend \
dummy_pre_up
up_body() { up_body() {
export IFACE=lo PHASE=up MOCK=echo export IFACE=lo PHASE=up MOCK=echo
@ -39,34 +40,34 @@ hwaddress_body() {
${EXECUTOR} ${EXECUTOR}
} }
vlan_explicit_up_body() { vlan_explicit_pre_up_body() {
export IFACE=servers PHASE=up MOCK=echo \ export IFACE=servers PHASE=pre-up MOCK=echo \
IF_VLAN_RAW_DEVICE="eth0" IF_VLAN_ID="123" IF_VLAN_RAW_DEVICE="eth0" IF_VLAN_ID="123"
atf_check -s exit:0 -o match:'ip link add link eth0 name servers type vlan id 123' \ atf_check -s exit:0 -o match:'ip link add link eth0 name servers type vlan id 123' \
${EXECUTOR} ${EXECUTOR}
} }
vlan_explicit_down_body() { vlan_explicit_post_down_body() {
export IFACE=servers PHASE=down MOCK=echo \ export IFACE=servers PHASE=post-down MOCK=echo \
IF_VLAN_RAW_DEVICE="eth0" IF_VLAN_ID="123" IF_VLAN_RAW_DEVICE="eth0" IF_VLAN_ID="123"
atf_check -s exit:0 -o match:'ip link delete link eth0 name servers type vlan id 123' \ atf_check -s exit:0 -o match:'ip link del servers' \
${EXECUTOR} ${EXECUTOR}
} }
vlan_guessed_up_body() { vlan_guessed_pre_up_body() {
export IFACE=eth0.8 PHASE=up MOCK=echo export IFACE=eth0.8 PHASE=pre-up MOCK=echo
atf_check -s exit:0 -o match:'ip link add link eth0 name eth0.8 type vlan id 8' \ atf_check -s exit:0 -o match:'ip link add link eth0 name eth0.8 type vlan id 8' \
${EXECUTOR} ${EXECUTOR}
} }
vlan_guessed_down_body() { vlan_guessed_post_down_body() {
export IFACE=eth0.8 PHASE=down MOCK=echo export IFACE=eth0.8 PHASE=post-down MOCK=echo
atf_check -s exit:0 -o match:'ip link delete link eth0 name eth0.8 type vlan id 8' \ atf_check -s exit:0 -o match:'ip link del eth0.8' \
${EXECUTOR} ${EXECUTOR}
} }
vlan_explicit_depend_body() { vlan_explicit_depend_body() {
export IFACE=servers PHASE=up MOCK=echo \ export IFACE=servers PHASE=depend \
IF_VLAN_RAW_DEVICE="eth0" IF_VLAN_ID="123" IF_VLAN_RAW_DEVICE="eth0" IF_VLAN_ID="123"
atf_check -s exit:0 -o match:'eth0' \ atf_check -s exit:0 -o match:'eth0' \
${EXECUTOR} ${EXECUTOR}
@ -77,3 +78,9 @@ vlan_guessed_depend_body() {
atf_check -s exit:0 -o match:'eth0' \ atf_check -s exit:0 -o match:'eth0' \
${EXECUTOR} ${EXECUTOR}
} }
dummy_pre_up_body() {
export IFACE=yolo IF_LINK_TYPE=dummy PHASE=pre-up MOCK=echo
atf_check -s exit:0 -o match:'ip link add yolo type dummy' \
${EXECUTOR}
}