From cf1f3ded9c4bcf9c9c8189b89f086c76b86cda93 Mon Sep 17 00:00:00 2001 From: Sapan Bhatia Date: Fri, 4 Dec 2009 15:21:21 +0000 Subject: [PATCH] Options support for vif_up, from Thom Haddow. --- exec/vif_up | 65 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 60 insertions(+), 5 deletions(-) diff --git a/exec/vif_up b/exec/vif_up index b40fef7..78fe86f 100755 --- a/exec/vif_up +++ b/exec/vif_up @@ -7,6 +7,7 @@ # - Interface name (eg [tun|tap]-) # - 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>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) -- 2.43.0