X-Git-Url: http://git.onelab.eu/?p=nodemanager-topo.git;a=blobdiff_plain;f=topo.py;h=2f7b6dd4b0ad0760e11cdf924a415b0a4a2f8179;hp=e142802772f22312d41298905c439a19f52ab4b9;hb=HEAD;hpb=1b5940a1a58122ab437c3b2dfe0794bbf01118c7 diff --git a/topo.py b/topo.py index e142802..2f7b6dd 100755 --- a/topo.py +++ b/topo.py @@ -13,6 +13,7 @@ import re import vserver import os from time import strftime +import socket dryrun = 0 vinidir = "/usr/share/vini/" @@ -30,6 +31,40 @@ def run(cmd): else: return subprocess.call(cmd, shell=True); +""" +From old pyplnet, former semantics needed for VINI +""" +def gifconf(): + try: + interfaces = os.listdir("/sys/class/net") + except: + interfaces = [] + s = None + ret = {} + try: + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0) + for interface in interfaces: + try: + ifreq = fcntl.ioctl(s.fileno(), SIOCGIFADDR, + struct.pack("16sH14x", interface, socket.AF_INET)) + (family, ip) = struct.unpack(SIOCGIFADDR_struct, ifreq) + if family == socket.AF_INET: + ret[interface] = _format_ip(ip) + else: + raise Exception + except: + ret[interface] = "0.0.0.0" + finally: + if s is not None: + s.close() + return ret + +""" +Subnet used for virtual interfaces by setup-egre-link script +""" +def iias_network(): + return "192.168.0.0 255.255.0.0" + """ Check for existence of interface dx @@ -46,7 +81,7 @@ Create a "virtual link" for slice between here and nodeid. The key is used to create the EGRE tunnel. """ def setup_virtual_link(slice, key, rate, myid, nodeid, ipaddr, virtip, vnet): - logger.log("%s: Set up virtual link to node %d" % (slice, nodeid)) + logger.log("%s: Set up virtual link to node %s" % (slice, nodeid)) run(setup_link_cmd + " %s %s %s %s %s %s %s" % (slice, nodeid, ipaddr, key, rate, virtip, vnet)) return @@ -71,6 +106,14 @@ def refresh_virtual_link(nodeid, key): return +""" +IP address of the NAT interface created inside the slice by the +setup-nat script. +""" +def nat_inner_ip(key): + return "10.0.%s.2" % key + + """ Check for existence of interface natx """ @@ -120,13 +163,13 @@ def clean_up_old_virtual_links(): for iface in old_ifaces: m = re.match(pattern1, iface) if m: - key = int(m.group(1)) - node = int(m.group(2)) + key = m.group(1) + node = m.group(2) teardown_virtual_link(key, node) m = re.match(pattern2, iface) if m: - key = int(m.group(1)) + key = m.group(1) teardown_nat(key) return @@ -152,6 +195,11 @@ def update_links(slice, myid, topospec, key, netns): logger.log("%s: virtual link to node %s exists" % (slice, nodeid)) refresh_virtual_link(nodeid, key) + +""" +Update NAT interface for the slice +""" +def update_nat(slice, myid, key, netns): if not nat_exists(key): if netns: setup_nat(slice, myid, key) @@ -161,11 +209,13 @@ def update_links(slice, myid, topospec, key, netns): """ -Write /etc/vservers//spaces/net +Write /etc/vservers//spaces/net. +Restart the vserver if there are any changes. """ -def writeConf(slicename, value): +def write_spaces_net(slicename, value): SLICEDIR="/etc/vservers/%s/" % slicename SPACESDIR="%s/spaces/" % SLICEDIR + FILENAME="%s/net" % SPACESDIR if os.path.exists(SLICEDIR): if not os.path.exists(SPACESDIR): try: @@ -173,13 +223,23 @@ def writeConf(slicename, value): except os.error: logger.log("topo: could not create %s\n" % SPACESDIR) return - f = open("%s/net" % SPACESDIR, "w") - f.write("%s\n" % value) - f.close() - STATUS="OFF" - if value: - STATUS="ON" - logger.log("%s: network namespace %s\n" % (slicename, STATUS)) + + if os.path.exists(FILENAME) != value: + sliver = vserver.VServer(slicename) + + sliver.stop() + + if value: + STATUS="ON" + f = open(FILENAME, "w") + f.close() + else: + STATUS="OFF" + os.remove(FILENAME) + + sliver.start() + + logger.log("%s: network namespace %s\n" % (slicename, STATUS)) """ @@ -211,17 +271,16 @@ password %s """ -Network used by OpenVPN on this node +IP address of NAT gateway to outside world """ -def openvpn_net(myid): - return "10.%s.0.0/16" % myid - +def nat_gw(key): + return "10.0.%s.1" % key """ -IP address of NAT gateway to outside world +IP address of the NAT interface inside the slice """ -def nat_gw(key, myid): - return "10.%s.%s.1" % (key, myid) +def nat_inner(key): + return "10.0.%s.2" % key """ @@ -241,13 +300,11 @@ link-detect """ % name) f.write ("""! -ip route %s %s -! access-list vty permit 127.0.0.1/32 ! line vty ! -""" % (openvpn_net(myid), nat_gw(key, myid))) +""") f.close() return @@ -259,6 +316,7 @@ def write_ospf(filename, myname, ifaces): f = open(filename, 'w') password = "zebra" write_header(f, myname, password) + name = None for name in ifaces: f.write ("""! @@ -269,7 +327,8 @@ def write_ospf(filename, myname, ifaces): ip ospf network non-broadcast """ % name) - f.write ("""! + if name: + f.write ("""! router ospf ospf router-id %s """ % ifaces[name]['local-ip']) @@ -281,7 +340,7 @@ def write_ospf(filename, myname, ifaces): net = ifaces[name]['network'] f.write (" network %s area 0\n" % net) - f.write(""" redistribute static + f.write(""" redistribute kernel ! access-list vty permit 127.0.0.1/32 ! @@ -299,7 +358,6 @@ def update_quagga_configs(slicename, hostname, myid, topo, key, netns): quagga_dir = "/vservers/%s/etc/quagga/" % slicename if not os.path.exists(quagga_dir): try: - # Quagga not installed. Install it here? Chkconfig, sym links. os.mkdir(quagga_dir) except os.error: logger.log("topo: could not create %s\n" % quagga_dir) @@ -309,8 +367,6 @@ def update_quagga_configs(slicename, hostname, myid, topo, key, netns): myid, key) write_ospf(quagga_dir + "ospfd.conf.generated", hostname, ifaces) - # Start up Quagga if we installed it earlier and netns = 1. - return @@ -324,8 +380,31 @@ def update_hosts(slicename, hosts): f.close() return +""" +Write /etc/vini/egre-keys.txt, used by vsys topo scripts +""" +def write_egre_keys(slicekeys): + vini_dir = "/etc/vini" + if not os.path.exists(vini_dir): + try: + os.mkdir(vini_dir) + except os.error: + logger.log("topo: could not create %s\n" % vini_dir) + return + keys_file = "%s/egre-keys.txt" % vini_dir + f = open(keys_file, 'w') + for slice in slicekeys: + f.write("%s %s\n" % (slice, slicekeys[slice])) + f.close() + return -def start(options, config): + +""" +Executed on NM startup +""" +def start(): + # Should be taken care of by /etc/sysctl.conf, but it doesn't hurt... + run ("echo 1 > /proc/sys/net/ipv4/ip_forward") pass @@ -336,29 +415,37 @@ an 'egre_key' attribute, and a 'topo_rspec' attribute. Creating the virtual link depends on the contents of /etc/vservers//spaces/net. Update this first. """ -def GetSlivers(data): +def GetSlivers(data, config = None, plc = None): global ifaces, old_ifaces - ifaces = old_ifaces = sioc.gifconf() + ifaces = old_ifaces = gifconf() + slicekeys = {} for sliver in data['slivers']: attrs = {} - for attribute in sliver['attributes']: - attrs[attribute['name']] = attribute['value'] - if 'netns' in attrs: - netns = int(attrs['netns']) - writeConf(sliver['name'], netns) - else: - netns = 0 + for tag in sliver['attributes']: + attrs[tag['tagname']] = tag['value'] + if tag['tagname'] == 'egre_key': + slicekeys[sliver['name']] = tag['value'] + + + if vserver.VServer(sliver['name']).is_running(): + if 'netns' in attrs: + netns = int(attrs['netns']) + else: + netns = 0 + write_spaces_net(sliver['name'], netns) if vserver.VServer(sliver['name']).is_running(): - if 'egre_key' in attrs and 'topo_rspec' in attrs: - logger.log("topo: Update topology for slice %s" % \ - sliver['name']) - update_links(sliver['name'], data['node_id'], - attrs['topo_rspec'], attrs['egre_key'], netns) - update_quagga_configs(sliver['name'], data['hostname'], - data['node_id'], attrs['topo_rspec'], - attrs['egre_key'], netns) + if 'egre_key' in attrs: + logger.log("topo: Update slice %s" % sliver['name']) + update_nat(sliver['name'], data['node_id'], attrs['egre_key'], + netns) + if 'topo_rspec' in attrs: + update_links(sliver['name'], data['node_id'], + attrs['topo_rspec'], attrs['egre_key'], netns) + update_quagga_configs(sliver['name'], data['hostname'], + data['node_id'], attrs['topo_rspec'], + attrs['egre_key'], netns) if 'hosts' in attrs: update_hosts(sliver['name'], attrs['hosts']) else: @@ -366,6 +453,7 @@ def GetSlivers(data): sliver['name']) clean_up_old_virtual_links() + write_egre_keys(slicekeys) return