5 # system provided modules
8 # PlanetLab system modules
12 import plnode.bwlimit as bwlimit
17 # we can't do anything without a network
20 dev_default = tools.get_default_if()
24 logger.log("net: plugin starting up...")
26 def GetSlivers(data, config, plc):
28 # band-aid for short period as old API returns networks instead of interfaces
30 KEY_NAME = "interfaces"
33 logger.verbose("net: GetSlivers called.")
34 if not 'interfaces' in data:
36 # band-aid for short period as old API returns networks instead of interfaces
37 # logger.log_missing_data('net.GetSlivers', 'interfaces')
39 if not 'networks' in data:
40 logger.log_missing_data('net.GetSlivers', 'interfaces')
46 plnet.InitInterfaces(logger, plc, data)
47 if 'OVERRIDES' in dir(config):
48 if config.OVERRIDES.get('net_max_rate') == '-1':
49 logger.log("net: Slice and node BW Limits disabled.")
50 if len(bwlimit.tc("class show dev %s" % dev_default)):
51 logger.verbose("net: *** DISABLING NODE BW LIMITS ***")
62 def InitNodeLimit(data):
64 # query running network interfaces
66 ips = dict(list(zip(list(devs.values()), list(devs.keys()))))
69 macs[sioc.gifhwaddr(dev).lower()] = dev
71 for interface in data[KEY_NAME]:
72 # Get interface name preferably from MAC address, falling
74 hwaddr=interface['mac']
75 if hwaddr != None: hwaddr=hwaddr.lower()
77 dev = macs[interface['mac']]
78 elif interface['ip'] in ips:
79 dev = ips[interface['ip']]
81 logger.log('net: %s: no such interface with address %s/%s' % (interface['hostname'], interface['ip'], interface['mac']))
84 # Get current node cap
86 old_bwlimit = bwlimit.get_bwcap(dev)
90 # Get desired node cap
91 if interface['bwlimit'] is None or interface['bwlimit'] < 0:
92 new_bwlimit = bwlimit.bwmax
94 new_bwlimit = interface['bwlimit']
96 if old_bwlimit != new_bwlimit:
97 # Reinitialize bandwidth limits
98 bwlimit.init(dev, new_bwlimit)
100 # XXX This should trigger an rspec refresh in case
101 # some previously invalid sliver bwlimit is now valid
102 # again, or vice-versa.
104 def InitI2(plc, data):
105 if not 'groups' in data: return
107 if "Internet2" in data['groups']:
108 logger.log("net: This is an Internet2 node. Setting rules.")
110 i2nodeids = plc.GetNodeGroups(["Internet2"])[0]['node_ids']
111 for node in plc.GetInterfaces({"node_id": i2nodeids}, ["ip"]):
113 i2nodes.append(node['ip'])
114 # this will create the set if it doesn't already exist
115 # and add IPs that don't exist in the set rather than
116 # just recreateing the set.
117 bwlimit.exempt_init('Internet2', i2nodes)
119 # set the iptables classification rule if it doesnt exist.
120 cmd = '-A POSTROUTING -m set --set Internet2 dst -j CLASSIFY --set-class 0001:2000 --add-mark'
122 ipt = os.popen("/sbin/iptables-save")
123 for line in ipt.readlines(): rules.append(line.strip(" \n"))
126 logger.verbose("net: Adding iptables rule for Internet2")
127 os.popen("/sbin/iptables -t mangle " + cmd)
129 def InitNAT(plc, data):
131 # query running network interfaces
132 devs = sioc.gifconf()
133 ips = dict(list(zip(list(devs.values()), list(devs.keys()))))
136 macs[sioc.gifhwaddr(dev).lower()] = dev
138 ipt = iptables.IPTables()
139 for interface in data[KEY_NAME]:
140 # Get interface name preferably from MAC address, falling
141 # back on IP address.
142 hwaddr=interface['mac']
143 if hwaddr != None: hwaddr=hwaddr.lower()
145 dev = macs[interface['mac']]
146 elif interface['ip'] in ips:
147 dev = ips[interface['ip']]
149 logger.log('net: %s: no such interface with address %s/%s' % (interface['hostname'], interface['ip'], interface['mac']))
153 settings = plc.GetInterfaceTags({'interface_tag_id': interface['interface_tag_ids']})
157 for setting in settings:
158 if setting['category'].upper() != 'FIREWALL':
160 if setting['name'].upper() == 'EXTERNAL':
161 # Enable NAT for this interface
163 elif setting['name'].upper() == 'INTERNAL':
165 elif setting['name'].upper() == 'PF': # XXX Uglier code is hard to find...
166 for pf in setting['value'].split("\n"):
168 for field in pf.split(","):
169 (key, val) = field.split("=", 2)
171 if 'new_dport' not in fields:
172 fields['new_dport'] = fields['dport']
173 if 'source' not in fields:
174 fields['source'] = "0.0.0.0/0"