Skip to content

Instantly share code, notes, and snippets.

@iximiuz
Last active January 13, 2024 22:17
Show Gist options
  • Save iximiuz/ca8dbed95013bcc2a87d325deb819760 to your computer and use it in GitHub Desktop.
Save iximiuz/ca8dbed95013bcc2a87d325deb819760 to your computer and use it in GitHub Desktop.
A bunch of helper functions to create Linux bridges, network namespaces, and interconnect everything using veth pairs.
#!/usr/bin/env bash
set -xeuo 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 bridge_nsname="$3"
local bridge_ifname="$4"
echo "Creating end host ${host_nsname} 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
# Attach peer2 interface to the bridge.
ip netns exec ${bridge_nsname} ip link set ${peer2_ifname} master ${bridge_ifname}
}
connect_bridges() {
local bridge1_nsname="$1"
local bridge1_ifname="$2"
local bridge2_nsname="$3"
local bridge2_ifname="$4"
local peer1_ifname="veth_${bridge2_ifname}"
local peer2_ifname="veth_${bridge1_ifname}"
echo "Connecting bridge ${bridge1_nsname}/${bridge1_ifname} to ${bridge2_nsname}/${bridge2_ifname} bridge using veth pair"
# Create veth pair.
ip link add ${peer1_ifname} netns ${bridge1_nsname} type veth peer \
${peer2_ifname} netns ${bridge2_nsname}
ip netns exec ${bridge1_nsname} ip link set ${peer1_ifname} up
ip netns exec ${bridge2_nsname} ip link set ${peer2_ifname} up
# Connect bridges.
ip netns exec ${bridge1_nsname} ip link set ${peer1_ifname} master ${bridge1_ifname}
ip netns exec ${bridge2_nsname} ip link set ${peer2_ifname} master ${bridge2_ifname}
}
# ---=== Scenario 1: single bridge ===---
setup__single_bridge() {
create_bridge netns_br0 br0
create_end_host netns_veth0 veth0 netns_br0 br0
create_end_host netns_veth1 veth1 netns_br0 br0
create_end_host netns_veth2 veth2 netns_br0 br0
}
teardown__single_bridge() {
ip netns delete netns_br0
ip netns delete netns_veth0
ip netns delete netns_veth1
ip netns delete netns_veth2
}
# ---=== Scenario 2: two bridges ===---
setup__two_bridges() {
create_bridge netns_br10 br10
create_end_host netns_veth10 veth10 netns_br10 br10
create_end_host netns_veth11 veth11 netns_br10 br10
create_bridge netns_br20 br20
create_end_host netns_veth20 veth20 netns_br20 br20
create_end_host netns_veth21 veth21 netns_br20 br20
connect_bridges netns_br10 br10 netns_br20 br20
}
teardown__two_bridges() {
ip netns delete netns_br10
ip netns delete netns_veth10
ip netns delete netns_veth11
ip netns delete netns_br20
ip netns delete netns_veth20
ip netns delete netns_veth21
}
# ---=== Scenario 3: hierarchical bridges ===---
setup__hierarchical_bridges() {
create_bridge netns_br10 br10
create_end_host netns_veth10 veth10 netns_br10 br10
create_end_host netns_veth11 veth11 netns_br10 br10
create_bridge netns_br20 br20
create_end_host netns_veth20 veth20 netns_br20 br20
create_end_host netns_veth21 veth21 netns_br20 br20
create_bridge netns_br30 br30
connect_bridges netns_br10 br10 netns_br30 br30
connect_bridges netns_br20 br20 netns_br30 br30
}
teardown__hierarchical_bridges() {
ip netns delete netns_br10
ip netns delete netns_veth10
ip netns delete netns_veth11
ip netns delete netns_br20
ip netns delete netns_veth20
ip netns delete netns_veth21
ip netns delete netns_br30
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment