-
-#
-#
"""network configuration"""
-import sioc
-import bwlimit
-import logger
-import string
+# system provided modules
+import os, string, time, socket
+from socket import inet_aton
+
+# PlanetLab system modules
+import sioc, plnet
+
+# local modules
+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():
+ 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:
+ # 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 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)):
+ logger.verbose("net: *** DISABLING NODE BW LIMITS ***")
+ bwlimit.stop()
+ else:
+ InitNodeLimit(data)
+ InitI2(plc, data)
+ else:
+ InitNodeLimit(data)
+ InitI2(plc, data)
+ InitNAT(plc, data)
+ """
+
+ InitPlanetStack(plc, data)
-def GetSlivers(plc, data):
- InitNodeLimit(data)
- InitI2(plc, data)
def InitNodeLimit(data):
+
# query running network interfaces
devs = sioc.gifconf()
ips = dict(zip(devs.values(), devs.keys()))
for dev in devs:
macs[sioc.gifhwaddr(dev).lower()] = dev
- # XXX Exempt Internet2 destinations from node bwlimits
- # bwlimit.exempt_init('Internet2', internet2_ips)
- for network in data['networks']:
+ for interface in data[KEY_NAME]:
# Get interface name preferably from MAC address, falling
# back on IP address.
- if macs.has_key(network['mac']):
- dev = macs[network['mac'].lower()]
- elif ips.has_key(network['ip']):
- dev = ips[network['ip']]
+ hwaddr=interface['mac']
+ if hwaddr <> None: hwaddr=hwaddr.lower()
+ if hwaddr in macs:
+ dev = macs[interface['mac']]
+ elif interface['ip'] in ips:
+ dev = ips[interface['ip']]
else:
- logger.log('%s: no such interface with address %s/%s' % (network['hostname'], network['ip'], network['mac']))
+ logger.log('net: %s: no such interface with address %s/%s' % (interface['hostname'], interface['ip'], interface['mac']))
continue
# Get current node cap
old_bwlimit = None
# Get desired node cap
- if network['bwlimit'] is None or network['bwlimit'] < 0:
+ if interface['bwlimit'] is None or interface['bwlimit'] < 0:
new_bwlimit = bwlimit.bwmax
else:
- new_bwlimit = network['bwlimit']
+ new_bwlimit = interface['bwlimit']
if old_bwlimit != new_bwlimit:
# Reinitialize bandwidth limits
# again, or vice-versa.
def InitI2(plc, data):
+ if not 'groups' in data: return
+
if "Internet2" in data['groups']:
- logger.log("This is an Internet2 node. Setting rules.")
+ logger.log("net: This is an Internet2 node. Setting rules.")
i2nodes = []
i2nodeids = plc.GetNodeGroups(["Internet2"])[0]['node_ids']
- for node in plc.GetNodeNetworks({"node_id": i2nodeids}, ["ip"]):
+ for node in plc.GetInterfaces({"node_id": i2nodeids}, ["ip"]):
+ # Get the IPs
i2nodes.append(node['ip'])
+ # this will create the set if it doesn't already exist
+ # and add IPs that don't exist in the set rather than
+ # just recreateing the set.
bwlimit.exempt_init('Internet2', i2nodes)
-def start(options, config):
- pass
+ # 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 = []
+ ipt = os.popen("/sbin/iptables-save")
+ for line in ipt.readlines(): rules.append(line.strip(" \n"))
+ ipt.close()
+ if cmd not in rules:
+ logger.verbose("net: Adding iptables rule for Internet2")
+ 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()))
+ macs = {}
+ for dev in devs:
+ macs[sioc.gifhwaddr(dev).lower()] = dev
+
+ ipt = iptables.IPTables()
+ for interface in data[KEY_NAME]:
+ # Get interface name preferably from MAC address, falling
+ # back on IP address.
+ hwaddr=interface['mac']
+ if hwaddr <> None: hwaddr=hwaddr.lower()
+ if hwaddr in macs:
+ dev = macs[interface['mac']]
+ elif interface['ip'] in ips:
+ dev = ips[interface['ip']]
+ else:
+ logger.log('net: %s: no such interface with address %s/%s' % (interface['hostname'], interface['ip'], interface['mac']))
+ continue
+
+ try:
+ settings = plc.GetInterfaceTags({'interface_tag_id': interface['interface_tag_ids']})
+ except:
+ continue
+
+ for setting in settings:
+ if setting['category'].upper() != 'FIREWALL':
+ continue
+ if setting['name'].upper() == 'EXTERNAL':
+ # Enable NAT for this interface
+ ipt.add_ext(dev)
+ elif setting['name'].upper() == 'INTERNAL':
+ ipt.add_int(dev)
+ elif setting['name'].upper() == 'PF': # XXX Uglier code is hard to find...
+ for pf in setting['value'].split("\n"):
+ fields = {}
+ for field in pf.split(","):
+ (key, val) = field.split("=", 2)
+ fields[key] = val
+ if 'new_dport' not in fields:
+ fields['new_dport'] = fields['dport']
+ if 'source' not in fields:
+ fields['source'] = "0.0.0.0/0"
+ ipt.add_pf(fields)
+ ipt.commit()
+
+# 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)