Merge branch 'planetstack' of ssh://git.planet-lab.org/git/nodemanager into planetstack
[nodemanager.git] / net.py
diff --git a/net.py b/net.py
index 760c1be..da33e54 100644 (file)
--- a/net.py
+++ b/net.py
@@ -1,35 +1,53 @@
-# $Id$
-# $URL$
-
 """network configuration"""
 
 # system provided modules
 import os, string, time, socket
+from socket import inet_aton
 
 # PlanetLab system modules
 import sioc, plnet
 
 # local modules
-import bwlimit, logger, iptables, tools
+import plnode.bwlimit as bwlimit
+import logger, iptables, tools
+import subprocess
 
 # we can't do anything without a network
 priority=1
 
 dev_default = tools.get_default_if()
 
-def start(options, conf):
+
+def start():
     logger.log("net: plugin starting up...")
 
 def GetSlivers(data, config, plc):
+    # added by caglar
+    # band-aid for short period as old API returns networks instead of interfaces
+    global KEY_NAME
+    KEY_NAME = "interfaces"
+    #################
+
     logger.verbose("net: GetSlivers called.")
-    if not 'interfaces' in data: 
-        logger.log_missing_data('net.GetSlivers','interfaces')
-        return
+    if not 'interfaces' in data:
+        # added by caglar
+        # band-aid for short period as old API returns networks instead of interfaces
+        # logger.log_missing_data('net.GetSlivers','interfaces')
+        # return
+        if not 'networks' in data:
+            logger.log_missing_data('net.GetSlivers','interfaces')
+            return
+        else:
+            KEY_NAME = "networks"
+        ##################
+
     plnet.InitInterfaces(logger, plc, data)
-    if 'OVERRIDES' in dir(config): 
+    
+    """
+    if 'OVERRIDES' in dir(config):
         if config.OVERRIDES.get('net_max_rate') == '-1':
             logger.log("net: Slice and node BW Limits disabled.")
-            if len(bwlimit.tc("class show dev %s" % dev_default)): 
+            if len(bwlimit.tc("class show dev %s" % dev_default)):
                 logger.verbose("net: *** DISABLING NODE BW LIMITS ***")
                 bwlimit.stop()
         else:
@@ -39,6 +57,9 @@ def GetSlivers(data, config, plc):
         InitNodeLimit(data)
         InitI2(plc, data)
     InitNAT(plc, data)
+    """
+    
+    InitPlanetStack(plc, data)
 
 
 def InitNodeLimit(data):
@@ -50,7 +71,7 @@ def InitNodeLimit(data):
     for dev in devs:
         macs[sioc.gifhwaddr(dev).lower()] = dev
 
-    for interface in data['interfaces']:
+    for interface in data[KEY_NAME]:
         # Get interface name preferably from MAC address, falling
         # back on IP address.
         hwaddr=interface['mac']
@@ -97,7 +118,7 @@ def InitI2(plc, data):
         # and add IPs that don't exist in the set rather than
         # just recreateing the set.
         bwlimit.exempt_init('Internet2', i2nodes)
-        
+
         # set the iptables classification rule if it doesnt exist.
         cmd = '-A POSTROUTING -m set --set Internet2 dst -j CLASSIFY --set-class 0001:2000 --add-mark'
         rules = []
@@ -109,7 +130,7 @@ def InitI2(plc, data):
             os.popen("/sbin/iptables -t mangle " + cmd)
 
 def InitNAT(plc, data):
-    
+
     # query running network interfaces
     devs = sioc.gifconf()
     ips = dict(zip(devs.values(), devs.keys()))
@@ -118,7 +139,7 @@ def InitNAT(plc, data):
         macs[sioc.gifhwaddr(dev).lower()] = dev
 
     ipt = iptables.IPTables()
-    for interface in data['interfaces']:
+    for interface in data[KEY_NAME]:
         # Get interface name preferably from MAC address, falling
         # back on IP address.
         hwaddr=interface['mac']
@@ -157,5 +178,109 @@ def InitNAT(plc, data):
                     ipt.add_pf(fields)
     ipt.commit()
 
-def start(options, config):
-    pass
+# Helper functions for converting to CIDR notation
+def get_net_size(netmask):
+    binary_str = ''
+    for octet in netmask:
+        binary_str += bin(int(octet))[2:].zfill(8)
+    return str(len(binary_str.rstrip('0')))
+
+def to_cidr(ipaddr, netmask):
+    # validate input
+    inet_aton(ipaddr)
+    inet_aton(netmask)
+
+    ipaddr = ipaddr.split('.')
+    netmask = netmask.split('.')
+
+    net_start = [str(int(ipaddr[x]) & int(netmask[x])) for x in range(0,4)]
+    return '.'.join(net_start) + '/' + get_net_size(netmask)
+
+def ipaddr_range(network, broadcast):
+    start = network.split('.')
+    end = broadcast.split('.')
+
+    # Assume interface always claims the first address in the block
+    start[3] = str(int(start[3]) + 2)
+    end[3] = str(int(end[3]) - 1)
+
+    return '.'.join(start) + ',' + '.'.join(end)
+
+def InitPlanetStack(plc, data):
+
+    for interface in data[KEY_NAME]:
+        try:
+            settings = plc.GetInterfaceTags({'interface_tag_id': interface['interface_tag_ids']})
+        except:
+            continue
+
+        tags = {}
+        for setting in settings:
+            tags[setting['tagname'].upper()] = setting['value']
+
+        if 'IFNAME' in tags:
+            dev = tags['IFNAME']
+        else:
+            # Skip devices that don't have names
+            logger.log('net:InitPlanetStack: Device has no name, skipping...')
+            continue
+
+        logger.log('net:InitPlanetStack: Processing device %s' % dev)
+        
+        if 'NAT' in tags:
+            # Enable iptables MASQ on this device
+            # Right now the subnet is hardcoded, should instead use interface's subnet
+            ipaddr = interface['ip']
+            netmask = interface['netmask']
+
+            if (ipaddr and netmask):
+                try:
+                    cidr = to_cidr(ipaddr, netmask)
+                except:
+                    logger.log('net:InitPlanetStack: could not convert ipaddr %s and netmask %s to CIDR' % (ipaddr, netmask))
+
+            if cidr:
+                cmd = ['/sbin/iptables', '-t', 'nat', '-C',  'POSTROUTING', '-s',  cidr, 
+                   '!',  '-d',  cidr, '-j', 'MASQUERADE']
+                try:
+                    logger.log('net:InitPlanetStack: checking if NAT iptables rule present for device %s' % dev)
+                    subprocess.check_call(cmd)
+                except:
+                    logger.log('net:InitPlanetStack: adding NAT iptables NAT for device %s' % dev)
+                    cmd[3] = '-A'
+                    try:
+                        subprocess.check_call(cmd)
+                    except:
+                        logger.log('net:InitPlanetStack: failed to add NAT iptables rule for device %s' % dev)
+                
+            # Enable dnsmasq for this interface
+            # Check if dnsmasq already running
+            start_dnsmasq = True
+            pidfile = '/var/run/dnsmasq-%s.pid' % dev
+            try:
+                pid = open(pidfile, 'r').read().strip()
+                if os.path.exists('/proc/%s' % pid):
+                    start_dnsmasq = False
+                    logger.log('net:InitPlanetStack: dnsmasq already running on device %s' % dev)
+            except:
+                pass
+
+            if start_dnsmasq:
+                try:
+                    logger.log('net:InitPlanetStack: starting dnsmasq on device %s' % dev)
+                    iprange = ipaddr_range(interface['network'], interface['broadcast'])
+                    logger.log('net:InitPlanetStack: IP range: %s' % iprange)
+                    subprocess.check_call(['/usr/sbin/dnsmasq', 
+                                           '--strict-order', 
+                                           '--bind-interfaces',
+                                           '--local=//',  
+                                           '--domain-needed',  
+                                           '--pid-file=%s' % pidfile, 
+                                           '--conf-file=',
+                                           '--interface=%s' % dev, 
+                                           '--dhcp-range=%s' % iprange, 
+                                           '--dhcp-leasefile=/var/lib/dnsmasq/%s.leases' % dev, 
+                                           '--dhcp-no-override'])
+                
+                except:
+                    logger.log('net:InitPlanetStack: failed to start dnsmasq for device %s' % dev)