Created
March 18, 2021 08:46
-
-
Save iximiuz/86012bd49433f71b4a19cb3745c3509d to your computer and use it in GitHub Desktop.
Sets up IP subnets over a single Ethernet broadcast domain formed by a Linux bridge.
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
#!/usr/bin/env bash | |
set -euo pipefail | |
create_bridge() { | |
local nsname="$1" | |
local ifname="$2" | |
echo "Creating bridge ${nsname}/${ifname}" | |
ip netns add ${nsname} | |
ip netns exec ${nsname} ip link set lo up | |
ip netns exec ${nsname} ip link add ${ifname} type bridge | |
ip netns exec ${nsname} ip link set ${ifname} up | |
} | |
create_end_host() { | |
local host_nsname="$1" | |
local peer1_ifname="$2a" | |
local peer2_ifname="$2b" | |
local peer1_ifaddr="$3" | |
local bridge_nsname="$4" | |
local bridge_ifname="$5" | |
echo "Creating end host ${host_nsname} ${peer1_ifaddr} connected to ${bridge_nsname}/${bridge_ifname} bridge" | |
# Create end host network namespace. | |
ip netns add ${host_nsname} | |
ip netns exec ${host_nsname} ip link set lo up | |
# Create a veth pair connecting end host and bridge namespaces. | |
ip link add ${peer1_ifname} netns ${host_nsname} type veth peer \ | |
${peer2_ifname} netns ${bridge_nsname} | |
ip netns exec ${host_nsname} ip link set ${peer1_ifname} up | |
ip netns exec ${bridge_nsname} ip link set ${peer2_ifname} up | |
# Setting host's IP address. | |
ip netns exec ${host_nsname} ip addr add ${peer1_ifaddr} dev ${peer1_ifname} | |
# Attach peer2 interface to the bridge. | |
ip netns exec ${bridge_nsname} ip link set ${peer2_ifname} master ${bridge_ifname} | |
} | |
# ---=== Scenario 1: 1:1 mapping ===--- | |
setup__one_to_one_mapping() { | |
create_bridge netns_br0 br0 | |
create_end_host netns_veth0 veth0 '192.168.0.5/24' netns_br0 br0 | |
create_end_host netns_veth1 veth1 '192.168.0.6/24' netns_br0 br0 | |
create_end_host netns_veth2 veth2 '192.168.0.7/24' netns_br0 br0 | |
} | |
teardown__one_to_one_mapping() { | |
ip netns delete netns_br0 | |
ip netns delete netns_veth0 | |
ip netns delete netns_veth1 | |
ip netns delete netns_veth2 | |
} | |
# ---=== Scenario 2: 1:N mapping ===--- | |
setup__one_to_many_mapping() { | |
create_bridge netns_br0 br0 | |
# Subnet 1 | |
create_end_host netns_veth0 veth0 192.168.0.5/24 netns_br0 br0 | |
create_end_host netns_veth1 veth1 192.168.0.6/24 netns_br0 br0 | |
create_end_host netns_veth2 veth2 192.168.0.7/24 netns_br0 br0 | |
# Subnet 2 | |
create_end_host netns_veth3 veth3 192.168.1.5/24 netns_br0 br0 | |
create_end_host netns_veth4 veth4 192.168.1.6/24 netns_br0 br0 | |
create_end_host netns_veth5 veth5 192.168.1.7/24 netns_br0 br0 | |
} | |
teardown__one_to_many_mapping() { | |
ip netns delete netns_br0 | |
ip netns delete netns_veth0 | |
ip netns delete netns_veth1 | |
ip netns delete netns_veth2 | |
ip netns delete netns_veth3 | |
ip netns delete netns_veth4 | |
ip netns delete netns_veth5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment