4 """network configuration"""
14 def GetSlivers(plc, data):
15 InitInterfaces(plc, data)
20 def InitNodeLimit(data):
21 # query running network interfaces
23 ips = dict(zip(devs.values(), devs.keys()))
26 macs[sioc.gifhwaddr(dev).lower()] = dev
28 for network in data['networks']:
29 # Get interface name preferably from MAC address, falling
31 if macs.has_key(network['mac'].lower()):
32 dev = macs[network['mac'].lower()]
33 elif ips.has_key(network['ip']):
34 dev = ips[network['ip']]
36 logger.log('%s: no such interface with address %s/%s' % (network['hostname'], network['ip'], network['mac']))
39 # Get current node cap
41 old_bwlimit = bwlimit.get_bwcap(dev)
45 # Get desired node cap
46 if network['bwlimit'] is None or network['bwlimit'] < 0:
47 new_bwlimit = bwlimit.bwmax
49 new_bwlimit = network['bwlimit']
51 if old_bwlimit != new_bwlimit:
52 # Reinitialize bandwidth limits
53 bwlimit.init(dev, new_bwlimit)
55 # XXX This should trigger an rspec refresh in case
56 # some previously invalid sliver bwlimit is now valid
57 # again, or vice-versa.
59 def InitI2(plc, data):
60 if "Internet2" in data['groups']:
61 logger.log("This is an Internet2 node. Setting rules.")
63 i2nodeids = plc.GetNodeGroups(["Internet2"])[0]['node_ids']
64 for node in plc.GetInterfaces({"node_id": i2nodeids}, ["ip"]):
66 i2nodes.append(node['ip'])
67 # this will create the set if it doesn't already exist
68 # and add IPs that don't exist in the set rather than
69 # just recreateing the set.
70 bwlimit.exempt_init('Internet2', i2nodes)
72 # set the iptables classification rule if it doesnt exist.
73 cmd = '-A POSTROUTING -m set --set Internet2 dst -j CLASSIFY --set-class 0001:2000 --add-mark'
75 ipt = os.popen("/sbin/iptables-save")
76 for line in ipt.readlines(): rules.append(line.strip(" \n"))
79 logger.verbose("net: Adding iptables rule for Internet2")
80 os.popen("/sbin/iptables -t mangle " + cmd)
82 def InitNAT(plc, data):
83 # query running network interfaces
85 ips = dict(zip(devs.values(), devs.keys()))
88 macs[sioc.gifhwaddr(dev).lower()] = dev
90 ipt = iptables.IPTables()
91 for network in data['networks']:
92 # Get interface name preferably from MAC address, falling
94 if macs.has_key(network['mac']):
95 dev = macs[network['mac'].lower()]
96 elif ips.has_key(network['ip']):
97 dev = ips[network['ip']]
99 logger.log('%s: no such interface with address %s/%s' % (network['hostname'], network['ip'], network['mac']))
103 settings = plc.GetInterfaceTags({'interface_tag_id': network['interface_tag_ids']})
106 # XXX arbitrary names
107 for setting in settings:
108 if setting['category'].upper() != 'FIREWALL':
110 if setting['name'].upper() == 'EXTERNAL':
111 # Enable NAT for this interface
113 elif setting['name'].upper() == 'INTERNAL':
115 elif setting['name'].upper() == 'PF': # XXX Uglier code is hard to find...
116 for pf in setting['value'].split("\n"):
118 for field in pf.split(","):
119 (key, val) = field.split("=", 2)
121 if 'new_dport' not in fields:
122 fields['new_dport'] = fields['dport']
123 if 'source' not in fields:
124 fields['source'] = "0.0.0.0/0"
128 def InitInterfaces(plc, data):
129 if not 'networks' in data:
131 plnet.InitInterfaces(logger, plc, data)
133 def start(options, config):