Decided to make this an API call.
[nodemanager-topo.git] / setup-egre-link
1 #!/bin/sh +x
2
3 IP=/sbin/ip
4
5 SLICE=$1
6 SLICEID=`id -u $SLICE`
7 NODEID=$2
8 REMOTE=$3
9 KEY=$4
10 RATE=$5
11 VIRTIP=$6
12 VIRTNET=$7
13
14 SUBNET=`expr match "$VIRTNET" '.*\(/[0-9]*\)'`
15 LINK=${KEY}x${NODEID}
16
17 modprobe ip_gre
18 modprobe etun
19
20 ### Setup EGRE tunnel
21 EGRE=d$LINK
22 $IP tunnel add $EGRE  mode gre/eth remote $REMOTE key $KEY ttl 64
23 $IP link set $EGRE up
24
25 ### Setup etun
26 ETUN0=a$LINK
27 ETUN1=b$LINK
28 echo $ETUN0,$ETUN1 > /sys/module/etun/parameters/newif
29 ifconfig $ETUN0 mtu 1458 up
30 ifconfig $ETUN1 up
31
32 ### Setup bridge
33 BRIDGE=c$LINK
34 brctl addbr $BRIDGE
35 brctl addif $BRIDGE $EGRE 
36 brctl addif $BRIDGE $ETUN1
37 ifconfig $BRIDGE up
38
39 ### Setup iptables so that packets are visible in the vserver
40 iptables -t mangle -A FORWARD -o $BRIDGE -j MARK --set-mark $SLICEID
41
42 ### Put a process in the vserver so we can move the interface there
43 su $SLICE -c "sleep 30" &
44 sleep 1
45 PID=`su $SLICE -c "pgrep sleep"`
46 chcontext --ctx 1 -- echo $PID > /sys/class/net/$ETUN0/new_ns_pid 
47 sleep 1
48 su $SLICE -c "sudo /sbin/ifconfig $ETUN0 ${VIRTIP}${SUBNET} up; sudo /sbin/route add -net $VIRTNET dev $ETUN0"
49
50 ### Set rate
51 tc qdisc add dev $EGRE root handle 1: htb default 10
52 tc class add dev $EGRE parent 1: classid 1:10 htb rate $RATE ceil $RATE
53