Scripts for creating and deleting NAT interface inside a sliver
[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
13 VIRTNET=`echo $VIRTIP|sed "s/\.[23]$/.0/"`
14 LINK=${KEY}x${NODEID}
15
16 modprobe ip_gre
17 modprobe etun
18
19 ### Setup EGRE tunnel
20 EGRE=d$LINK
21 $IP tunnel add $EGRE  mode gre/eth remote $REMOTE key $KEY ttl 64
22 $IP link set $EGRE up
23
24 ### Setup etun
25 ETUN0=a$LINK
26 ETUN1=b$LINK
27 echo $ETUN0,$ETUN1 > /sys/module/etun/parameters/newif
28 ifconfig $ETUN0 mtu 1458 up
29 ifconfig $ETUN1 up
30
31 ### Setup bridge
32 BRIDGE=c$LINK
33 brctl addbr $BRIDGE
34 brctl addif $BRIDGE $EGRE 
35 brctl addif $BRIDGE $ETUN1
36 ifconfig $BRIDGE up
37
38 ### Setup iptables so that packets are visible in the vserver
39 iptables -t mangle -A FORWARD -o $BRIDGE -j MARK --set-mark $SLICEID
40
41 ### Put a process in the vserver so we can move the interface there
42 su $SLICE -c "sleep 30" &
43 sleep 1
44 PID=`su $SLICE -c "pgrep sleep"`
45 chcontext --ctx 1 -- echo $PID > /sys/class/net/$ETUN0/new_ns_pid 
46 sleep 1
47 su $SLICE -c "sudo /sbin/ifconfig $ETUN0 $VIRTIP/24 up; sudo /sbin/route add -net $VIRTNET/24 dev $ETUN0"
48
49 ### Set rate
50 tc qdisc add dev $EGRE root handle 1: htb default 10
51 tc class add dev $EGRE parent 1: classid 1:10 htb rate $RATE ceil $RATE
52