X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=net.py;h=16953aae2809cbaba841107cdf8834f7d44ef027;hb=16f4fdef39711a31d09485e289a779b68fc5970a;hp=1a6ec3527128717b8bf20e22cc2f6c5724f45261;hpb=1e0ebd0efc059b6a0ce82ae7754205e95b0b7518;p=nodemanager.git diff --git a/net.py b/net.py index 1a6ec35..16953aa 100644 --- a/net.py +++ b/net.py @@ -1,10 +1,21 @@ +# +# $Id$ +# """network configuration""" import sioc import bwlimit import logger +import string +import iptables +import os -def GetSlivers(data): +def GetSlivers(plc, data): + InitNodeLimit(data) + InitI2(plc, data) + InitNAT(plc, data) + +def InitNodeLimit(data): # query running network interfaces devs = sioc.gifconf() ips = dict(zip(devs.values(), devs.keys())) @@ -14,7 +25,6 @@ def GetSlivers(data): # XXX Exempt Internet2 destinations from node bwlimits # bwlimit.exempt_init('Internet2', internet2_ips) - for network in data['networks']: # Get interface name preferably from MAC address, falling # back on IP address. @@ -23,7 +33,7 @@ def GetSlivers(data): elif ips.has_key(network['ip']): dev = ips[network['ip']] else: - logger.log('%s: no such interface with address %s/%s' % (self.name, network['ip'], network['mac'])) + logger.log('%s: no such interface with address %s/%s' % (network['hostname'], network['ip'], network['mac'])) continue # Get current node cap @@ -46,5 +56,74 @@ def GetSlivers(data): # some previously invalid sliver bwlimit is now valid # again, or vice-versa. +def InitI2(plc, data): + if "Internet2" in data['groups']: + logger.log("This is an Internet2 node. Setting rules.") + i2nodes = [] + i2nodeids = plc.GetNodeGroups(["Internet2"])[0]['node_ids'] + 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) + + # 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 network in data['networks']: + # 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']] + else: + logger.log('%s: no such interface with address %s/%s' % (network['hostname'], network['ip'], network['mac'])) + continue + + try: + settings = plc.GetInterfaceSettings({'interface_setting_id': network['interface_setting_ids']}) + except: + continue + # XXX arbitrary names + 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() + def start(options, config): pass