From: "Robin H. Johnson" <robbat2@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/netifrc:master commit in: net/
Date: Sun, 21 Apr 2019 04:11:59 +0000 (UTC) [thread overview]
Message-ID: <1555819841.963d378276193c9ae73b889da03960258773d259.robbat2@OpenRC> (raw)
commit: 963d378276193c9ae73b889da03960258773d259
Author: Kirill Semenkov <semenkovk <AT> gmail <DOT> com>
AuthorDate: Mon Dec 17 09:07:18 2018 +0000
Commit: Robin H. Johnson <robbat2 <AT> gentoo <DOT> 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 <robbat2 <AT> 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
+}
next reply other threads:[~2019-04-21 4:12 UTC|newest]
Thread overview: 112+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-04-21 4:11 Robin H. Johnson [this message]
-- strict thread matches above, loose matches on Subject: below --
2025-03-18 21:50 [gentoo-commits] proj/netifrc:master commit in: net/ Sam James
2025-03-18 21:50 Sam James
2025-03-18 21:50 Sam James
2024-09-29 0:28 Sam James
2024-09-23 12:02 Sam James
2024-09-09 22:43 Patrick McLean
2024-09-01 9:22 Sam James
2024-09-01 9:21 Sam James
2024-08-28 18:54 Patrick McLean
2024-06-11 0:39 Patrick McLean
2024-06-11 0:39 Patrick McLean
2024-05-23 18:12 Patrick McLean
2023-11-26 5:31 Robin H. Johnson
2023-11-25 4:52 Robin H. Johnson
2023-10-19 22:57 Sam James
2023-09-10 15:15 Sam James
2023-05-29 0:17 Mike Gilbert
2023-04-19 17:14 Robin H. Johnson
2023-04-17 4:35 Sam James
2023-02-12 6:54 Sam James
2023-01-19 18:50 Sam James
2023-01-19 18:50 Sam James
2023-01-17 15:06 Sam James
2023-01-17 15:06 Sam James
2023-01-17 15:06 Sam James
2023-01-15 20:37 Sam James
2023-01-15 14:03 Sam James
2023-01-15 14:03 Sam James
2023-01-15 1:53 Sam James
2023-01-15 1:51 Sam James
2023-01-15 1:51 Sam James
2022-12-25 19:14 Robin H. Johnson
2022-12-25 19:07 Robin H. Johnson
2021-03-31 1:11 Patrick McLean
2021-03-11 16:54 Lars Wendler
2021-03-11 16:54 Lars Wendler
2021-03-11 16:54 Lars Wendler
2021-02-02 8:28 Lars Wendler
2021-01-27 16:55 Lars Wendler
2021-01-27 14:19 Lars Wendler
2021-01-27 14:06 Lars Wendler
2021-01-24 10:37 Lars Wendler
2021-01-18 14:40 Lars Wendler
2021-01-18 13:04 Lars Wendler
2021-01-18 12:33 Lars Wendler
2021-01-18 12:33 Lars Wendler
2021-01-05 14:27 Lars Wendler
2021-01-05 12:46 Lars Wendler
2021-01-05 12:46 Lars Wendler
2020-12-15 20:11 Lars Wendler
2020-06-02 21:54 Robin H. Johnson
2020-05-31 6:05 Robin H. Johnson
2020-05-31 5:42 Robin H. Johnson
2020-05-31 5:29 Robin H. Johnson
2020-05-31 5:29 Robin H. Johnson
2020-05-31 5:13 Robin H. Johnson
2020-05-31 5:13 Robin H. Johnson
2020-01-04 8:06 Robin H. Johnson
2020-01-04 7:59 Robin H. Johnson
2019-07-09 19:59 Ben Kohler
2019-07-09 19:59 Ben Kohler
2019-07-09 17:39 Ben Kohler
2018-07-12 6:25 Robin H. Johnson
2018-07-10 21:11 Jason Donenfeld
2018-01-21 21:59 Robin H. Johnson
2017-11-27 20:22 Robin H. Johnson
2017-11-27 20:22 Robin H. Johnson
2017-11-14 20:48 Robin H. Johnson
2017-11-14 20:48 Robin H. Johnson
2017-10-21 20:56 Robin H. Johnson
2017-06-09 23:59 Robin H. Johnson
2017-06-09 23:55 Robin H. Johnson
2017-06-09 23:52 Robin H. Johnson
2017-06-09 23:52 Robin H. Johnson
2017-01-27 21:54 Robin H. Johnson
2016-10-27 1:23 Robin H. Johnson
2016-10-24 23:16 Robin H. Johnson
2016-10-24 20:58 Robin H. Johnson
2016-10-24 16:49 Robin H. Johnson
2016-10-24 0:25 Robin H. Johnson
2016-10-24 0:23 Robin H. Johnson
2016-10-24 0:23 Robin H. Johnson
2016-10-24 0:04 Robin H. Johnson
2016-10-24 0:04 Robin H. Johnson
2016-10-23 23:50 Robin H. Johnson
2016-10-23 23:50 Robin H. Johnson
2016-10-23 23:50 Robin H. Johnson
2016-10-23 22:39 Robin H. Johnson
2016-10-23 22:39 Robin H. Johnson
2016-10-05 14:42 Robin H. Johnson
2016-07-19 19:59 Robin H. Johnson
2016-07-05 18:14 Robin H. Johnson
2016-07-05 18:14 Robin H. Johnson
2016-07-05 18:14 Robin H. Johnson
2016-07-05 18:09 Robin H. Johnson
2015-11-08 14:33 Robin H. Johnson
2015-11-08 14:30 Robin H. Johnson
2015-05-25 10:04 Mike Frysinger
2015-05-25 10:04 Mike Frysinger
2015-05-25 10:04 Mike Frysinger
2015-05-25 10:01 Mike Frysinger
2015-01-22 23:01 Robin H. Johnson
2015-01-22 23:01 Robin H. Johnson
2014-12-11 18:13 Robin H. Johnson
2014-09-24 6:19 Robin H. Johnson
2014-07-17 17:56 Robin H. Johnson
2014-07-17 17:56 Robin H. Johnson
2014-04-15 18:18 Robin H. Johnson
2013-08-28 16:59 Robin H. Johnson
2013-08-27 20:06 Ian Stakenvicius
2013-08-25 8:17 Robin H. Johnson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1555819841.963d378276193c9ae73b889da03960258773d259.robbat2@OpenRC \
--to=robbat2@gentoo.org \
--cc=gentoo-commits@lists.gentoo.org \
--cc=gentoo-dev@lists.gentoo.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox