From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 2FDAC138335 for ; Sun, 21 Apr 2019 04:12:05 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 0AE4BE0B66; Sun, 21 Apr 2019 04:12:04 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id AAC2FE0B65 for ; Sun, 21 Apr 2019 04:12:03 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id C6CBA341BA7 for ; Sun, 21 Apr 2019 04:12:01 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id D771C574 for ; Sun, 21 Apr 2019 04:11:59 +0000 (UTC) From: "Robin H. Johnson" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Robin H. Johnson" Message-ID: <1555819841.963d378276193c9ae73b889da03960258773d259.robbat2@OpenRC> Subject: [gentoo-commits] proj/netifrc:master commit in: net/ X-VCS-Repository: proj/netifrc X-VCS-Files: net/veth.sh X-VCS-Directories: net/ X-VCS-Committer: robbat2 X-VCS-Committer-Name: Robin H. Johnson X-VCS-Revision: 963d378276193c9ae73b889da03960258773d259 X-VCS-Branch: master Date: Sun, 21 Apr 2019 04:11:59 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: 566283a7-98ce-43b0-b864-1fada770775e X-Archives-Hash: 3d9c4d51c27626e4fa273fc050b67fdc commit: 963d378276193c9ae73b889da03960258773d259 Author: Kirill Semenkov gmail com> AuthorDate: Mon Dec 17 09:07:18 2018 +0000 Commit: Robin H. Johnson gentoo org> CommitDate: Sun Apr 21 04:10:41 2019 +0000 URL: https://gitweb.gentoo.org/proj/netifrc.git/commit/?id=963d3782 Veth support added Signed-off-by: Robin H. Johnson gentoo.org> net/veth.sh | 193 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 193 insertions(+) diff --git a/net/veth.sh b/net/veth.sh new file mode 100644 index 0000000..d969a14 --- /dev/null +++ b/net/veth.sh @@ -0,0 +1,193 @@ +# Copyright (c) 2018 +# Released under the 2-clause BSD license. + +veth_depend() +{ + program ip awk +} + +_config_vars="$_config_vars veth" + + +# We need it because _exists() function seeks in /sys/class/net +_netns_exists() +{ + [ -e "/var/run/netns/$1" ] +} + + +#Creates the network namespace if it doesn't exist. If called with no arguments, does nothing +#Arguments: +# $1 - name of the namespace +_create_ns() { + + vethrc=0 + for _ns in "$@"; do + if [ -z "$_ns" ]; then + continue + fi + if ! _netns_exists "$_ns"; then + ip netns add "$_ns" > /dev/null 2>&1 + vethrc=$(($?+ vethrc)) + fi + done + return $vethrc +} + +#Brings a virtual interface up and takes network namespaces into account +#Arguments: +# $1 - name of the interface, required! +# $2 - namespace +_bring_peer_up() +{ + if [ ! -z "$2" ]; then + ip link set "$1" netns "$2" > /dev/null 2>&1 + vethrc=$? + ip netns exec "$2" ip link set dev "$1" up > /dev/null 2>&1 + vethrc=$(($?+ vethrc)) + return $vethrc + fi + + ip link set dev "$1" up > /dev/null 2>&1 + return $? +} + +#Brings a virtual interface down and takes network namespaces into account +#Arguments: +# $1 - name of the interface, required! +# $2 - namespace +_bring_peer_down() +{ + + if [ ! -z "$2" ]; then + ip netns exec "$2" ip link del dev "$1" > /dev/null 2>&1 + return $? + fi + + ip link del dev "$1" > /dev/null 2>&1 + return $? +} + + +#Create and bring the veth pair up +_create_peers() +{ + local peers + peers="$(_get_array "veth_${IFVAR}")" + + # veth has exactly two peers. + # For POSIX compatibility we evade bash arrays + local npeers + npeers=$(echo "$peers" | awk '{print NF}') + if [ "$npeers" != 2 ]; then + eerror "veth interface must have exactly two peers" + return 1 + fi + + for x in ${peers}; do + if _exists "$x" ; then + eerror "Interface $x already exists. Can't continue" + return 1 + fi + done + + local netns1 + netns1="$(_get_array "veth_${IFVAR}_ns1")" + local netns2 + netns2="$(_get_array "veth_${IFVAR}_ns2")" + + local vethrc + + if ! _create_ns "$netns1" "$netns2" + then + eerror "Can't create namespaces: $netns1 $netns2" + return 1 + fi + + local peer1 + peer1=$(echo "$peers" | awk '{print $1}') + local peer2 + peer2=$(echo "$peers" | awk '{print $2}') + + + ip link add "$peer1" type veth peer name "$peer2" > /dev/null 2>&1 || { + eerror "Can't create veth peer $peer1 or $peer2" + return 1 + } + + + if ! _bring_peer_up "$peer1" "$netns1" + then + eerror "Can't bring the veth peer $peer1 up" + return 1 + + fi + if ! _bring_peer_up "$peer2" "$netns2" + then + eerror "Can't bring the veth peer $peer2 up" + return 1 + + fi + + return 0 +} + +# Create peers and namespaces +veth_pre_start() +{ + local itype + eval itype=\$type_${IFVAR} + if [ "$itype" != "veth" ]; then + return 0 + fi + + local createveth + eval createveth=\$veth_${IFVAR}_create + if [ "$createveth" == "no" ]; then + return 0 + fi + + type ip >/dev/null 2>&1 || { + eerror "iproute2 nor found, please install iproute2" + return 1 + } + + if ! _create_peers + then + return 1 + fi + + + return 0 +} + +#Delete the veth pair +#We don't delete namespaces because someone may use them for some purposes +veth_post_stop() +{ + local itype + eval itype=\$type_${IFVAR} + if [ "$itype" != "veth" ]; then + return 0 + fi + + local createveth + eval createveth=\$veth_${IFVAR}_create + if [ "$createveth" == "no" ]; then + return 0 + fi + + local peers + peers="$(_get_array "veth_${IFVAR}")" + local first + first=$(echo "$peers" | awk '{print $1}') + local netns1 + netns1="$(_get_array "veth_${IFVAR}_ns1")" + + if ! _bring_peer_down "$first" "$netns1" + then + eerror "Can't delete the veth pair ${IFVAR}" + eend 1 + fi + return 0 +}