add support for creating gre tunnels
[vsys-scripts.git] / exec / vif_up
index b40fef7..33cbb22 100755 (executable)
@@ -5,8 +5,20 @@
 # Gets slice name as argv[1]
 # Takes remaining args on stdin:
 #   - Interface name (eg [tun|tap]<sliceid>-<n>)
-#   - IP address (eg 1.2.3.4)
+#   - (private) IP address (eg 1.2.3.4)
 #   - Netmask (as int, e.g. 24)
+#   - Followed by options as name=value pairs
+#
+# Options:
+#   - pointopoint=IP: other endpoint's (private) IP (sets routing)
+#   - snat=1: enables SNAT ip rules
+#   - txqueuelen=N: sets TX queue
+#   - gre=<> : enable GRE tunnelling - several formats supported
+#   -   gre=true|yes|name : computes GRE key as a hash of slice name (so it's valid across federations)
+#   -   gre=id            : uses for GRE key the slice id (the one from the myplc database)
+#   -   gre=somestring    : computes GRE key as a hash of the provided string
+#   -   gre=somekey       : use the provided key as is
+#   - remote=IP : when using GRE, the (public) IP for the remote endpoint
 
 import sys
 import pwd
@@ -14,6 +26,7 @@ import re
 import socket
 import struct
 import os
+import string
 
 vsys_config_dir = "/etc/planetlab/vsys-attributes"
 
@@ -21,6 +34,7 @@ if len(sys.argv) != 2: sys.exit(1)
 
 # VSYS scripts get slicename as $1
 slicename=sys.argv[1]
+# the local xid for this slice
 sliceid = pwd.getpwnam(slicename).pw_uid
 
 netblock_config=os.path.join(vsys_config_dir,slicename,"vsys_vnet")
@@ -37,13 +51,21 @@ if base is None:
 
 
 ### Read args from stdin
-vif = sys.stdin.readline().strip() # interface name
-vip = sys.stdin.readline().strip() # IP
-vmask = int(sys.stdin.readline().strip()) # netmask as int
+arglines = map(string.strip, sys.stdin.readlines())
 
-# TODO further config args? txqueue, nat? Can add these later...
+if len(arglines)<3:
+    print >>sys.stderr, "Insufficient argument lines."
+    sys.exit(1)
 
+vif = arglines[0] # interface name
+vip = arglines[1] # IP
+vmask = int(arglines[2]) # netmask as int
 
+# Create options list
+if len(arglines)>3:
+    options = arglines[3:]
+else:
+    options = []
 
 # Convert network base addr to int format by unpacking as 32bit net-ordered long (!L)
 base_int = struct.unpack('!L',socket.inet_aton(base))[0]
@@ -87,18 +109,157 @@ if vmask<mask:
 
 
 
+### Process options
+
+opt_txqueuelen = None
+opt_rp_filter = None
+opt_snat = None
+opt_ovs_dp = None
+opt_pointopoint = None
+opt_gre = None
+opt_gre_remote = None
+
+
+for optionline in options:
+    if len(optionline)==0: continue
+    try:
+        opt, val = optionline.split('=')
+    except:
+        print >>sys.stderr, "Bad option line: \"%s\"" % (optionline)
+        sys.exit(1)
+
+    if opt=="rp_filter":
+        if val=="0":
+            opt_rp_filter="0"
+        elif val=="1":
+            opt_rp_filter="1"
+        else:
+            print >>sys.stderr, "rp_filter value invalid: \"%s\"" % (val)
+            sys.exit(1)
+
+    elif opt=="txqueuelen":
+        intval = int(val)
+        if intval<1 or intval>10000:
+            print >>sys.stderr, "txqueuelen value %s out of range 1-10000" % (val)
+            sys.exit(1)
+        opt_txqueuelen = intval
+    elif opt=="snat":
+        intval = int(val)
+        if val=="1":
+            opt_snat = True
+    elif opt=="pointopoint":
+        opt_pointopoint = val.strip()
+        try:
+            socket.inet_aton(opt_pointopoint)
+        except socket.error,e:
+            print >>sys.stderr, "could not parse pointopoint: %s" % (e,)
+            sys.exit(1)
+    elif opt=="vswitch":
+        opt_ovs_dp = val
+    # support several formats, parse later on
+    elif opt=="gre":
+        opt_gre = val
+    elif opt=="remote":
+        opt_gre_remote = val.strip()
+        try:
+            socket.inet_aton(opt_gre_remote)
+        except socket.error,e:
+            print >>sys.stderr, "could not parse remote: %s" % (e,)
+            sys.exit(1)
+    else:
+        print >>sys.stderr, "Unknown option: \"%s\"" % (opt)
+        sys.exit(1)
+
+# post processing for gre mode
+if opt_gre:
+    if not opt_pointopoint:
+        print >>sys.stderr, "GRE tunnels need a pointopoint address"
+        sys.exit(1)
+    if not opt_gre_remote:
+        print >>sys.stderr, "GRE tunnels need a pointopoint address"
+        sys.exit(1)
+
+    if vif.startswith('tun'):
+        opt_gre_type = 'gre'
+    else:
+        opt_gre_type = 'gretap'
+
+    opt_gre=opt_gre.lower()
+    # helper - keys are expected between 0 and 2**32-1
+    def gre_hash (string):
+        return hash(string) & 0xffffffff
+    # compute gre_key according to option format
+    if opt_gre in ('yes','true','name'):
+        gre_key=gre_hash(slicename)
+    elif opt_gre in ('id','sliceid'):
+        # use slice_id as the key - this is the one from myplc - won't work across federation
+        gre_key = int(open("/etc/vservers/%s/slice_id" % (slicename,),"r").read().strip())
+    elif opt_gre.isdigit():
+        # use as-is
+        gre_key=int(opt_gre)
+    else:
+        # hash the provided string
+        gre_key=gre_hash(opt_gre)
+    ### temporary - debug 
+    ### use ip tunnel show to obtain the key, but it only works with IP over GRE
+    #open('/tmp/%s.key'%vif,'w').write('%s\n'%gre_key)
+
+
 ### Configure interface
 
-cmd_ifconfig = "/sbin/ifconfig %s %s/%d" % (vif, vip, vmask)
-os.system(cmd_ifconfig)
+cmd_ifconfig = "/sbin/ifconfig %s %s" % (vif, vip)
+if opt_pointopoint is None:
+    cmd_ifconfig += "/%d" % (vmask,)
+else:
+    # point-to-point mask
+    cmd_ifconfig += " netmask 255.255.255.255"
+if opt_txqueuelen is not None:
+    cmd_ifconfig += " txqueuelen %d" % (opt_txqueuelen,)
+if opt_pointopoint is not None:
+    cmd_ifconfig += " pointopoint %s" % (opt_pointopoint,)
 
 # Add iptables rules (Clearing old ones first, if they exist)
-cmd_iptables_in = "/sbin/iptables -A INPUT -i %s -m mark ! --mark %d -j DROP" % (vif, sliceid)
-cmd_iptables_del_in = "/sbin/iptables -D INPUT -i %s -m mark ! --mark %d -j DROP 2>/dev/null" % (vif, sliceid)
-cmd_iptables_out = "/sbin/iptables -A OUTPUT -o %s -m mark ! --mark %d -j DROP" % (vif, sliceid)
-cmd_iptables_del_out = "/sbin/iptables -D OUTPUT -o %s -m mark ! --mark %d -j DROP 2>/dev/null" % (vif, sliceid)
+cmd_iptables_in = "/sbin/iptables -A INPUT -i %s -m mark -m state --state NEW ! --mark %d -j DROP" % (vif, sliceid)
+cmd_iptables_del_in = "/sbin/iptables -D INPUT -i %s -m mark -m state --state NEW ! --mark %d -j DROP 2>/dev/null" % (vif, sliceid)
+cmd_iptables_out = "/sbin/iptables -A OUTPUT -o %s -m state --state NEW -m mark ! --mark %d -j DROP" % (vif, sliceid)
+cmd_iptables_del_out = "/sbin/iptables -D OUTPUT -o %s -m state --state NEW -m mark ! --mark %d -j DROP 2>/dev/null" % (vif, sliceid)
+
+# this initial form should work again now, but for some reason on 2.6.27, 'ip route get ...' returns an empty line
+#public_src = os.popen("ip route get 1.1.1.1 | head -1 | awk '{print $7;}'").read().rstrip();
+public_src = os.popen("ifconfig | grep $(ip route | grep default | awk '{print $3}' | awk -F. '{print $1\"[.]\"$2}') | head -1 | awk '{print $2}' | awk -F : '{print $2}'").read().rstrip()
+cmd_iptables_pr = "/sbin/iptables -t nat -A POSTROUTING -s %s/%d -j SNAT --to-source %s --random" % (vip, vmask, public_src)
+cmd_iptables_del_pr = "/sbin/iptables -t nat -D POSTROUTING -s %s/%d -j SNAT --to-source %s --random > /dev/null 2>&1" % (vip, vmask, public_src)
+
+
+if opt_gre:
+    cmd_gre_setup = "modprobe ip_gre ; ip link add %s type %s remote %s local %s ttl 64 csum key %s" % (
+        vif, opt_gre_type, opt_gre_remote, public_src, gre_key )
+    cmd_iptables_gre_pr = "/sbin/iptables -t mangle -A INPUT -i %s -m mark --mark 0 -j MARK --set-mark %s 2>/dev/null" % (
+        vif, int(sliceid))
+
+    os.system(cmd_gre_setup)
+    os.system(cmd_iptables_gre_pr)
+
+os.system(cmd_ifconfig)
 
 os.system(cmd_iptables_del_in)
 os.system(cmd_iptables_in)
 os.system(cmd_iptables_del_out)
 os.system(cmd_iptables_out)
+
+# always remove snat rules
+# in case there are leftovers from previous calls
+os.system(cmd_iptables_del_pr)
+if (opt_snat):
+    os.system(cmd_iptables_pr)
+
+# Process additional options
+if opt_rp_filter is not None:
+    rp_cmd = "/sbin/sysctl net.ipv4.conf.%s.rp_filter=%s" % (vif, opt_rp_filter)
+    os.system(rp_cmd)
+
+# OVS datapath
+if opt_ovs_dp is not None:
+    cmd_ovs_addif = "ovs-dpctl add-if %s %s"%(opt_ovs_dp,vif)
+    os.system(cmd_ovs_addif)
+