Last active
June 20, 2022 03:54
-
-
Save naoki9911/770f092d08c66b8109effbe7fe313dce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Thanks to https://github.com/skjune12/srv6-playground | |
#!/bin/bash | |
if [[ $(id -u) -ne 0 ]] ; then | |
echo "Please run with sudo" | |
exit 1 | |
fi | |
run () { | |
echo "$@" | |
"$@" || exit 1 | |
} | |
create_router1 () { | |
# setup namespaces | |
run ip netns add host1 | |
run ip netns add router1 | |
# setup veth peer | |
run ip link add veth-h1-rt1 type veth peer name veth-rt1-h1 | |
run ip link set veth-h1-rt1 netns host1 | |
run ip link set veth-rt1-h1 netns router1 | |
# host1 configuraiton | |
run ip netns exec host1 ip link set lo up | |
run ip netns exec host1 ip addr add fc00:a::2/64 dev veth-h1-rt1 | |
run ip netns exec host1 ip addr add 2001:db8:1::2/64 dev veth-h1-rt1 | |
run ip netns exec host1 ip link set veth-h1-rt1 up | |
run ip netns exec host1 ip -6 route add fc00::1/128 via fc00:a::1 | |
run ip netns exec host1 ip -6 route add fc00::2/128 via fc00:a::1 | |
run ip netns exec host1 ip -6 route add fc00::3/128 via fc00:a::1 | |
run ip netns exec host1 ip -6 route add 2001:db8:2::/64 via fc00:a::1 | |
# router1 configuration | |
run ip netns exec router1 ip link set lo up | |
run ip netns exec router1 ip -6 a add fc00::1/128 dev lo | |
run ip netns exec router1 ip link set veth-rt1-h1 up | |
run ip netns exec router1 ip addr add fc00:a::1/64 dev veth-rt1-h1 | |
run ip netns exec router1 ip addr add 2001:db8:1::1/64 dev veth-rt1-h1 | |
# sysctl for router1 | |
ip netns exec router1 sysctl net.ipv6.conf.all.forwarding=1 | |
ip netns exec router1 sysctl net.ipv6.conf.all.seg6_enabled=1 | |
ip netns exec router1 sysctl net.ipv6.conf.veth-rt1-h1.seg6_enabled=1 | |
} | |
create_router2 () { | |
# setup namespaces | |
run ip netns add router2 | |
# router2 configuration | |
run ip netns exec router2 ip link set lo up | |
run ip netns exec router2 ip -6 a add fc00::2/128 dev lo | |
# sysctl for router2 | |
ip netns exec router2 sysctl net.ipv6.conf.all.forwarding=1 | |
ip netns exec router2 sysctl net.ipv6.conf.all.seg6_enabled=1 | |
} | |
create_router3 () { | |
# setup namespaces | |
run ip netns add host2 | |
run ip netns add router3 | |
# setup veth peer | |
run ip link add veth-h2-rt3 type veth peer name veth-rt3-h2 | |
run ip link set veth-h2-rt3 netns host2 | |
run ip link set veth-rt3-h2 netns router3 | |
# host2 configuraiton | |
run ip netns exec host2 ip link set lo up | |
run ip netns exec host2 ip addr add fc00:c::2/64 dev veth-h2-rt3 | |
run ip netns exec host2 ip addr add 2001:db8:2::2/64 dev veth-h2-rt3 | |
run ip netns exec host2 ip link set veth-h2-rt3 up | |
run ip netns exec host2 ip -6 route add fc00::1/128 via fc00:c::1 | |
run ip netns exec host2 ip -6 route add fc00::2/128 via fc00:c::1 | |
run ip netns exec host2 ip -6 route add fc00::3/128 via fc00:c::1 | |
run ip netns exec host2 ip -6 route add 2001:db8:1::/64 via fc00:c::1 | |
# router3 configuration | |
run ip netns exec router3 ip link set lo up | |
run ip netns exec router3 ip -6 a add fc00::3/128 dev lo | |
run ip netns exec router3 ip link set veth-rt3-h2 up | |
run ip netns exec router3 ip addr add fc00:c::1/64 dev veth-rt3-h2 | |
run ip netns exec router3 ip addr add 2001:db8:2::1/64 dev veth-rt3-h2 | |
# sysctl for router3 | |
ip netns exec router3 sysctl net.ipv6.conf.all.forwarding=1 | |
ip netns exec router3 sysctl net.ipv6.conf.all.seg6_enabled=1 | |
ip netns exec router3 sysctl net.ipv6.conf.veth-rt3-h2.seg6_enabled=1 | |
} | |
connect_rt1_rt2 () { | |
# create veth peer | |
run ip link add veth-rt1-rt2 type veth peer name veth-rt2-rt1 | |
run ip link set veth-rt1-rt2 netns router1 | |
run ip link set veth-rt2-rt1 netns router2 | |
# configure router1 | |
ip netns exec router1 sysctl net.ipv6.conf.veth-rt1-rt2.seg6_enabled=1 | |
run ip netns exec router1 ip link set veth-rt1-rt2 up | |
run ip netns exec router1 ip addr add fc00:12::1/64 dev veth-rt1-rt2 | |
run ip netns exec router1 ip -6 route add fc00::2/128 via fc00:12::2 | |
run ip netns exec router1 ip -6 route add fc00::3/128 via fc00:12::2 | |
run ip netns exec router1 ip -6 route add 2001:db8:2::/64 encap seg6 mode encap segs fc00::3 dev veth-rt1-h1 | |
# configure router2 | |
ip netns exec router2 sysctl net.ipv6.conf.veth-rt2-rt1.seg6_enabled=1 | |
run ip netns exec router2 ip link set veth-rt2-rt1 up | |
run ip netns exec router2 ip addr add fc00:12::2/64 dev veth-rt2-rt1 | |
run ip netns exec router2 ip -6 route add fc00::1/128 via fc00:12::1 | |
} | |
connect_rt2_rt3 () { | |
# create veth peer | |
run ip link add veth-rt2-rt3 type veth peer name veth-rt3-rt2 | |
run ip link set veth-rt2-rt3 netns router2 | |
run ip link set veth-rt3-rt2 netns router3 | |
# configure router2 | |
run ip netns exec router2 ip link set veth-rt2-rt3 up | |
ip netns exec router2 sysctl net.ipv6.conf.veth-rt2-rt3.seg6_enabled=1 | |
run ip netns exec router2 ip addr add fc00:23::1/64 dev veth-rt2-rt3 | |
run ip netns exec router2 ip -6 route add fc00::3/128 via fc00:23::2 | |
# configure router3 | |
run ip netns exec router3 ip link set veth-rt3-rt2 up | |
ip netns exec router3 sysctl net.ipv6.conf.veth-rt3-rt2.seg6_enabled=1 | |
run ip netns exec router3 ip addr add fc00:23::2/64 dev veth-rt3-rt2 | |
run ip netns exec router3 ip -6 route add fc00::1/128 via fc00:23::1 | |
run ip netns exec router3 ip -6 route add fc00::2/128 via fc00:23::1 | |
run ip netns exec router3 ip -6 route add 2001:db8:1::/64 encap seg6 mode encap segs fc00::1 dev veth-rt3-h2 | |
} | |
destroy_network () { | |
run ip netns del router1 | |
run ip netns del host1 | |
run ip netns del router2 | |
run ip netns del router3 | |
run ip netns del host2 | |
} | |
stop () { | |
destroy_network | |
} | |
trap stop 0 1 2 3 13 14 15 | |
# exec functions | |
create_router1 | |
create_router2 | |
create_router3 | |
connect_rt1_rt2 | |
connect_rt2_rt3 | |
status=0; $SHELL || status=$? | |
exit $status |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment