4 """network configuration"""
6 # system provided modules
7 import os, string, time, socket
9 # PlanetLab system modules
13 import bwlimit, logger, iptables, tools
15 # we can't do anything without a network
18 dev_default = tools.get_default_if()
22 logger.log("net: plugin starting up...")
24 def GetSlivers(data, config, plc):
26 # band-aid for short period as old API returns networks instead of interfaces
28 KEY_NAME = "interfaces"
31 logger.verbose("net: GetSlivers called.")
32 if not 'interfaces' in data:
34 # band-aid for short period as old API returns networks instead of interfaces
35 # logger.log_missing_data('net.GetSlivers','interfaces')
37 if not 'networks' in data:
38 logger.log_missing_data('net.GetSlivers','interfaces')
44 plnet.InitInterfaces(logger, plc, data)
45 if 'OVERRIDES' in dir(config):
46 if config.OVERRIDES.get('net_max_rate') == '-1':
47 logger.log("net: Slice and node BW Limits disabled.")
48 if len(bwlimit.tc("class show dev %s" % dev_default)):
49 logger.verbose("net: *** DISABLING NODE BW LIMITS ***")
60 def InitNodeLimit(data):
62 # query running network interfaces
64 ips = dict(zip(devs.values(), devs.keys()))
67 macs[sioc.gifhwaddr(dev).lower()] = dev
69 for interface in data[KEY_NAME]:
70 # Get interface name preferably from MAC address, falling
72 hwaddr=interface['mac']
73 if hwaddr <> None: hwaddr=hwaddr.lower()
75 dev = macs[interface['mac']]
76 elif interface['ip'] in ips:
77 dev = ips[interface['ip']]
79 logger.log('net: %s: no such interface with address %s/%s' % (interface['hostname'], interface['ip'], interface['mac']))
82 # Get current node cap
84 old_bwlimit = bwlimit.get_bwcap(dev)
88 # Get desired node cap
89 if interface['bwlimit'] is None or interface['bwlimit'] < 0:
90 new_bwlimit = bwlimit.bwmax
92 new_bwlimit = interface['bwlimit']
94 if old_bwlimit != new_bwlimit:
95 # Reinitialize bandwidth limits
96 bwlimit.init(dev, new_bwlimit)
98 # XXX This should trigger an rspec refresh in case
99 # some previously invalid sliver bwlimit is now valid
100 # again, or vice-versa.
102 def InitI2(plc, data):
103 if not 'groups' in data: return
105 if "Internet2" in data['groups']:
106 logger.log("net: This is an Internet2 node. Setting rules.")
108 i2nodeids = plc.GetNodeGroups(["Internet2"])[0]['node_ids']
109 for node in plc.GetInterfaces({"node_id": i2nodeids}, ["ip"]):
111 i2nodes.append(node['ip'])
112 # this will create the set if it doesn't already exist
113 # and add IPs that don't exist in the set rather than
114 # just recreateing the set.
115 bwlimit.exempt_init('Internet2', i2nodes)
117 # set the iptables classification rule if it doesnt exist.
118 cmd = '-A POSTROUTING -m set --set Internet2 dst -j CLASSIFY --set-class 0001:2000 --add-mark'
120 ipt = os.popen("/sbin/iptables-save")
121 for line in ipt.readlines(): rules.append(line.strip(" \n"))
124 logger.verbose("net: Adding iptables rule for Internet2")
125 os.popen("/sbin/iptables -t mangle " + cmd)
127 def InitNAT(plc, data):
129 # query running network interfaces
130 devs = sioc.gifconf()
131 ips = dict(zip(devs.values(), devs.keys()))
134 macs[sioc.gifhwaddr(dev).lower()] = dev
136 ipt = iptables.IPTables()
137 for interface in data[KEY_NAME]:
138 # Get interface name preferably from MAC address, falling
139 # back on IP address.
140 hwaddr=interface['mac']
141 if hwaddr <> None: hwaddr=hwaddr.lower()
143 dev = macs[interface['mac']]
144 elif interface['ip'] in ips:
145 dev = ips[interface['ip']]
147 logger.log('net: %s: no such interface with address %s/%s' % (interface['hostname'], interface['ip'], interface['mac']))
151 settings = plc.GetInterfaceTags({'interface_tag_id': interface['interface_tag_ids']})
155 for setting in settings:
156 if setting['category'].upper() != 'FIREWALL':
158 if setting['name'].upper() == 'EXTERNAL':
159 # Enable NAT for this interface
161 elif setting['name'].upper() == 'INTERNAL':
163 elif setting['name'].upper() == 'PF': # XXX Uglier code is hard to find...
164 for pf in setting['value'].split("\n"):
166 for field in pf.split(","):
167 (key, val) = field.split("=", 2)
169 if 'new_dport' not in fields:
170 fields['new_dport'] = fields['dport']
171 if 'source' not in fields:
172 fields['source'] = "0.0.0.0/0"