4 """network configuration"""
6 # system provided modules
7 import os, string, time, socket
9 # PlanetLab system modules
13 import bwlimit, logger, iptables, tools
15 dev_default = tools.get_default_if()
17 def start(options, conf):
18 logger.log("net plugin starting up...")
20 def GetSlivers(data, config, plc):
21 logger.verbose("net:GetSlivers called.")
22 InitInterfaces(plc, data) # writes sysconfig files.
23 if 'OVERRIDES' in dir(config):
24 if config.OVERRIDES.get('net_max_rate') == '-1':
25 logger.log("net: Slice and node BW Limits disabled.")
26 if len(bwlimit.tc("class show dev %s" % dev_default)):
27 logger.verbose("*** DISABLING NODE BW LIMITS ***")
38 def InitNodeLimit(data):
39 if not 'networks' in data: return
41 # query running network interfaces
43 ips = dict(zip(devs.values(), devs.keys()))
46 macs[sioc.gifhwaddr(dev).lower()] = dev
48 for network in data['networks']:
49 # Get interface name preferably from MAC address, falling
52 if hwaddr <> None: hwaddr=hwaddr.lower()
54 dev = macs[network['mac']]
55 elif network['ip'] in ips:
56 dev = ips[network['ip']]
58 logger.log('%s: no such interface with address %s/%s' % (network['hostname'], network['ip'], network['mac']))
61 # Get current node cap
63 old_bwlimit = bwlimit.get_bwcap(dev)
67 # Get desired node cap
68 if network['bwlimit'] is None or network['bwlimit'] < 0:
69 new_bwlimit = bwlimit.bwmax
71 new_bwlimit = network['bwlimit']
73 if old_bwlimit != new_bwlimit:
74 # Reinitialize bandwidth limits
75 bwlimit.init(dev, new_bwlimit)
77 # XXX This should trigger an rspec refresh in case
78 # some previously invalid sliver bwlimit is now valid
79 # again, or vice-versa.
81 def InitI2(plc, data):
82 if not 'groups' in data: return
84 if "Internet2" in data['groups']:
85 logger.log("This is an Internet2 node. Setting rules.")
87 i2nodeids = plc.GetNodeGroups(["Internet2"])[0]['node_ids']
88 for node in plc.GetInterfaces({"node_id": i2nodeids}, ["ip"]):
90 i2nodes.append(node['ip'])
91 # this will create the set if it doesn't already exist
92 # and add IPs that don't exist in the set rather than
93 # just recreateing the set.
94 bwlimit.exempt_init('Internet2', i2nodes)
96 # set the iptables classification rule if it doesnt exist.
97 cmd = '-A POSTROUTING -m set --set Internet2 dst -j CLASSIFY --set-class 0001:2000 --add-mark'
99 ipt = os.popen("/sbin/iptables-save")
100 for line in ipt.readlines(): rules.append(line.strip(" \n"))
103 logger.verbose("net: Adding iptables rule for Internet2")
104 os.popen("/sbin/iptables -t mangle " + cmd)
106 def InitNAT(plc, data):
107 if not 'networks' in data: return
109 # query running network interfaces
110 devs = sioc.gifconf()
111 ips = dict(zip(devs.values(), devs.keys()))
114 macs[sioc.gifhwaddr(dev).lower()] = dev
116 ipt = iptables.IPTables()
117 for network in data['networks']:
118 # Get interface name preferably from MAC address, falling
119 # back on IP address.
120 hwaddr=network['mac']
121 if hwaddr <> None: hwaddr=hwaddr.lower()
123 dev = macs[network['mac']]
124 elif network['ip'] in ips:
125 dev = ips[network['ip']]
127 logger.log('%s: no such interface with address %s/%s' % (network['hostname'], network['ip'], network['mac']))
131 settings = plc.GetInterfaceTags({'interface_tag_id': network['interface_tag_ids']})
135 for setting in settings:
136 if setting['category'].upper() != 'FIREWALL':
138 if setting['name'].upper() == 'EXTERNAL':
139 # Enable NAT for this interface
141 elif setting['name'].upper() == 'INTERNAL':
143 elif setting['name'].upper() == 'PF': # XXX Uglier code is hard to find...
144 for pf in setting['value'].split("\n"):
146 for field in pf.split(","):
147 (key, val) = field.split("=", 2)
149 if 'new_dport' not in fields:
150 fields['new_dport'] = fields['dport']
151 if 'source' not in fields:
152 fields['source'] = "0.0.0.0/0"
156 def InitInterfaces(plc, data):
157 if not 'networks' in data: return
158 plnet.InitInterfaces(logger, plc, data)
160 def start(options, config):