-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.bash
128 lines (116 loc) · 4.36 KB
/
helper.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
######################
## Helper functions ##
######################
# Handle the arguments given to build and run command
handle_arguments() {
if (( $# != 2 ))
then
echo "Usage: ./demo $1 <target>"
echo " <target> has to be one of:"
for i in "${USABLE_SWITCHES[@]}"; do echo " - $i"; done
exit 1
fi
if [[ " ${USABLE_SWITCHES[*]} " =~ " $2 " ]]
then
SWITCH=$2
else
echo "Error: Unknown target."
echo "Usage: ./demo $1 <target>"
echo " <target> has to be one of:"
for i in "${USABLE_SWITCHES[@]}"; do echo " - $i"; done
exit 1
fi
}
# Set the underlay IP and port of a SCION link.
set_link_underlay() {
local as_a=$1
local underlay_a=$2
local as_b=$3
local underlay_b=$4
local as_a_config=$(isd_as_to_conf_dir $as_a)
local as_b_config=$(isd_as_to_conf_dir $as_b)
jq "(.border_routers[].interfaces[] | select(.\"isd_as\" == \"$as_b\") | .underlay) = {\"public\": \"$underlay_a\", \"remote\": \"$underlay_b\"}" \
${SCION_ROOT}/gen/$as_a_config/topology.json | sponge ${SCION_ROOT}/gen/$as_a_config/topology.json
jq "(.border_routers[].interfaces[] | select(.\"isd_as\" == \"$as_a\") | .underlay) = {\"public\": \"$underlay_b\", \"remote\": \"$underlay_a\"}" \
${SCION_ROOT}/gen/$as_b_config/topology.json | sponge ${SCION_ROOT}/gen/$as_b_config/topology.json
}
# Set the address of the SCION control and discovery services.
set_control_addr() {
local as=$1
local address=$2
local as_config=$(isd_as_to_conf_dir $as)
cat ${SCION_ROOT}/gen/$as_config/topology.json | jq ".control_service[].addr=\"$address\"" \
| jq ".discovery_service[].addr=\"$address\"" | sponge ${SCION_ROOT}/gen/$as_config/topology.json
}
# Set the internal address of a border router.
set_br_internal_addr() {
local as=$1
local br=$2
local address=$3
local as_config=$(isd_as_to_conf_dir $as)
jq ".border_routers.\"$br\".internal_addr = \"$address\"" \
${SCION_ROOT}/gen/$as_config/topology.json | sponge ${SCION_ROOT}/gen/$as_config/topology.json
}
# Set the IP address of the SCION daemon.
set_scion_daemon_address() {
local as_config=$1
local sd_address=$2
tomlq -t ".sd.address=\"$sd_address\"" ${SCION_ROOT}/gen/$as_config/sd.toml | sponge ${SCION_ROOT}/gen/$as_config/sd.toml
}
# Convert an ISD-AS pair (e.g., "1-ff00:0:1") to the corresponding configuration directory
# (e.g., "ASff00_0_1").
isd_as_to_conf_dir() {
echo $1 | sed -r 's/[0-9]-([0-9a-f]+):([0-9a-f]+):([0-9a-f]+)/AS\1_\2_\3/' -
}
# Makes the network namespace of a docker container visible to 'ip netns'.
mount_netns() {
local cntr=$1
local pid=$(docker inspect -f '{{.State.Pid}}' $cntr)
sudo mkdir -p /var/run/netns
sudo touch /var/run/netns/$cntr
sudo mount --bind /proc/$pid/ns/net /var/run/netns/$cntr
}
# Cleans up the bind mount created by mount_netns.
umount_netns(){
local cntr=$1
sudo umount /var/run/netns/$cntr
sudo rm /var/run/netns/$cntr
}
# Create a veth pair connecting two network namespaces.
create_veth() {
local veth0=$1
local ns0=$2
local ip0=$3
local veth1=$4
local ns1=$5
local ip1=$6
sudo ip link add $veth0 netns $ns0 type veth peer name $veth1 netns $ns1
sudo ip netns exec $ns0 ip add add dev $veth0 $ip0
sudo ip netns exec $ns0 ip link set dev $veth0 up
sudo ip netns exec $ns1 ip add add dev $veth1 $ip1
sudo ip netns exec $ns1 ip link set dev $veth1 up
}
# Create a veth pair connection the global namespace to another namespace.
create_veth_global_ns() {
local veth0=$1
local veth1=$2
local ns1=$3
local ip1=$4
sudo ip link add $veth0 type veth peer name $veth1 netns $ns1
sudo ip netns exec $ns1 ip add add dev $veth1 $ip1
sudo ip netns exec $ns1 ip link set dev $veth1 up
sudo ip link set dev $veth0 up
}
# Delete a veth pair.
delete_veth() {
sudo ip link del $1
}
# Configure iptables to always compute UDP/TCP checksum for outgoing packets on the given interface.
force_chksum_update() {
local cntr=$1
local interface=$2
docker exec -u root $cntr \
iptables -t mangle -A POSTROUTING -o $interface -p udp -m udp -j CHECKSUM --checksum-fill
docker exec -u root $cntr \
iptables -t mangle -A POSTROUTING -o $interface -p tcp -m tcp -j CHECKSUM --checksum-fill
}