5 """network configuration"""
7 # system provided modules
8 import os, string, time, socket
10 # PlanetLab system modules
14 import bwlimit, logger, iptables, tools
16 dev_default = tools.get_default_if()
18 def start(options, conf):
19 logger.log("net plugin starting up...")
21 def GetSlivers(data, config, plc):
22 logger.verbose("net:GetSlivers called.")
23 InitInterfaces(plc, data) # writes sysconfig files.
24 if 'OVERRIDES' in dir(config):
25 if config.OVERRIDES.get('net_max_rate') == '-1':
26 logger.log("net: Slice and node BW Limits disabled.")
27 if len(bwlimit.tc("class show dev %s" % dev_default)):
28 logger.verbose("*** DISABLING NODE BW LIMITS ***")
39 def InitNodeLimit(data):
40 if not 'networks' in data: return
42 # query running network interfaces
44 ips = dict(zip(devs.values(), devs.keys()))
47 macs[sioc.gifhwaddr(dev).lower()] = dev
49 for network in data['networks']:
50 # Get interface name preferably from MAC address, falling
53 if hwaddr <> None: hwaddr=hwaddr.lower()
55 dev = macs[network['mac']]
56 elif network['ip'] in ips:
57 dev = ips[network['ip']]
59 logger.log('%s: no such interface with address %s/%s' % (network['hostname'], network['ip'], network['mac']))
62 # Get current node cap
64 old_bwlimit = bwlimit.get_bwcap(dev)
68 # Get desired node cap
69 if network['bwlimit'] is None or network['bwlimit'] < 0:
70 new_bwlimit = bwlimit.bwmax
72 new_bwlimit = network['bwlimit']
74 if old_bwlimit != new_bwlimit:
75 # Reinitialize bandwidth limits
76 bwlimit.init(dev, new_bwlimit)
78 # XXX This should trigger an rspec refresh in case
79 # some previously invalid sliver bwlimit is now valid
80 # again, or vice-versa.
82 def InitI2(plc, data):
83 if not 'groups' in data: return
85 if "Internet2" in data['groups']:
86 logger.log("This is an Internet2 node. Setting rules.")
88 i2nodeids = plc.GetNodeGroups(["Internet2"])[0]['node_ids']
89 for node in plc.GetInterfaces({"node_id": i2nodeids}, ["ip"]):
91 i2nodes.append(node['ip'])
92 # this will create the set if it doesn't already exist
93 # and add IPs that don't exist in the set rather than
94 # just recreateing the set.
95 bwlimit.exempt_init('Internet2', i2nodes)
97 # set the iptables classification rule if it doesnt exist.
98 cmd = '-A POSTROUTING -m set --set Internet2 dst -j CLASSIFY --set-class 0001:2000 --add-mark'
100 ipt = os.popen("/sbin/iptables-save")
101 for line in ipt.readlines(): rules.append(line.strip(" \n"))
104 logger.verbose("net: Adding iptables rule for Internet2")
105 os.popen("/sbin/iptables -t mangle " + cmd)
107 def InitNAT(plc, data):
108 if not 'networks' in data: return
110 # query running network interfaces
111 devs = sioc.gifconf()
112 ips = dict(zip(devs.values(), devs.keys()))
115 macs[sioc.gifhwaddr(dev).lower()] = dev
117 ipt = iptables.IPTables()
118 for network in data['networks']:
119 # Get interface name preferably from MAC address, falling
120 # back on IP address.
121 hwaddr=network['mac']
122 if hwaddr <> None: hwaddr=hwaddr.lower()
124 dev = macs[network['mac']]
125 elif network['ip'] in ips:
126 dev = ips[network['ip']]
128 logger.log('%s: no such interface with address %s/%s' % (network['hostname'], network['ip'], network['mac']))
132 settings = plc.GetInterfaceTags({'interface_tag_id': network['interface_tag_ids']})
136 for setting in settings:
137 if setting['category'].upper() != 'FIREWALL':
139 if setting['name'].upper() == 'EXTERNAL':
140 # Enable NAT for this interface
142 elif setting['name'].upper() == 'INTERNAL':
144 elif setting['name'].upper() == 'PF': # XXX Uglier code is hard to find...
145 for pf in setting['value'].split("\n"):
147 for field in pf.split(","):
148 (key, val) = field.split("=", 2)
150 if 'new_dport' not in fields:
151 fields['new_dport'] = fields['dport']
152 if 'source' not in fields:
153 fields['source'] = "0.0.0.0/0"
157 def InitInterfaces(plc, data):
158 if not 'networks' in data: return
159 plnet.InitInterfaces(logger, plc, data)
161 def start(options, config):