Options support for vif_up, from Thom Haddow.
authorSapan Bhatia <sapanb@cs.princeton.edu>
Fri, 4 Dec 2009 15:21:21 +0000 (15:21 +0000)
committerSapan Bhatia <sapanb@cs.princeton.edu>
Fri, 4 Dec 2009 15:21:21 +0000 (15:21 +0000)
exec/vif_up

index b40fef7..78fe86f 100755 (executable)
@@ -7,6 +7,7 @@
 #   - Interface name (eg [tun|tap]<sliceid>-<n>)
 #   - IP address (eg 1.2.3.4)
 #   - Netmask (as int, e.g. 24)
+#   - Followed by options as name=value pairs
 
 import sys
 import pwd
@@ -14,6 +15,7 @@ import re
 import socket
 import struct
 import os
+import string
 
 vsys_config_dir = "/etc/planetlab/vsys-attributes"
 
@@ -37,13 +39,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,9 +97,49 @@ if vmask<mask:
 
 
 
+### Process options
+
+opt_txqueuelen = None
+opt_rp_filter = 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
+
+    else:
+        print >>sys.stderr, "Unknown option: \"%s\"" % (opt)
+        sys.exit(1)
+
+
+        
 ### Configure interface
 
-cmd_ifconfig = "/sbin/ifconfig %s %s/%d" % (vif, vip, vmask)
+if opt_txqueuelen is None:
+    cmd_ifconfig = "/sbin/ifconfig %s %s/%d" % (vif, vip, vmask)
+else:
+    cmd_ifconfig = "/sbin/ifconfig %s %s/%d txqueuelen %d" % (vif, vip, vmask, opt_txqueuelen)
+
 os.system(cmd_ifconfig)
 
 # Add iptables rules (Clearing old ones first, if they exist)
@@ -102,3 +152,8 @@ os.system(cmd_iptables_del_in)
 os.system(cmd_iptables_in)
 os.system(cmd_iptables_del_out)
 os.system(cmd_iptables_out)
+
+# 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)