Add virtual IP addresses to 'rspec', advertise OpenVPN address block
authorAndy Bavier <acb@cs.princeton.edu>
Tue, 10 Mar 2009 18:26:20 +0000 (18:26 +0000)
committerAndy Bavier <acb@cs.princeton.edu>
Tue, 10 Mar 2009 18:26:20 +0000 (18:26 +0000)
NodeManager-topo.spec
create-topo-attributes.py
setup-egre-link
topo.py

index 3131307..6090c43 100644 (file)
@@ -1,8 +1,8 @@
 %define url $URL$
 
 Name:          NodeManager-topo
-Version:       0.2
-Release:       2
+Version:       0.3
+Release:       1
 Summary:       Plugin supporting creating a default virtual topology.
 
 Group:         System Environment/Daemons
index 05b5aba..c446b94 100755 (executable)
@@ -10,50 +10,25 @@ import string
 import socket
 from topology import links
 
-"""
-Generate site adjacency map from list of links
-"""
-def gen_adjacencies(links):
-    adj = {}
-    for (a, b) in links:
-        if a in adj:
-            adj[a].append(b)
-        else:
-            adj[a] = [a, b]
-        if b in adj:
-            adj[b].append(a)
-        else:
-            adj[b] = [b, a]
-    return adj
-
-
-"""
-Test whether two sites are adjacent to each other in the adjacency graph.
-"""
-def is_adjacent(adjacencies, s1, s2):
-    set1 = set(adjacencies[s1])
-    set2 = set(adjacencies[s2])
-
-    if s1 in set2 and s2 in set1:
-        return True
-    elif not s1 in set2 and not s2 in set1:
-        return False
-    else:
-        raise Exception("Adjacency mismatch, sites %d and %d." % (s1, s2))
-
-
-"""
-Check the adjacency graph for discrepancies.
-"""
-def check_adjacencies(adjacencies):
-    for site in adjacencies:
-        for adj in adjacencies[site]:
-            try:
-                test = is_adjacent(adjacencies, site, adj)
-            except Exception, e:
-                print "Error: ", e, " Fix adjacencies!"
-    return
 
+def get_adjacency_matrix(links):
+    topo = {}
+    idx = 1
+    for (a, b) in links:
+        aNodes = get_sitenodes(a)
+        bNodes = get_sitenodes(b)
+        for nodeA in aNodes:
+            for nodeB in bNodes:
+                if nodeA not in topo:
+                    topo[nodeA] = {}
+                if nodeB not in topo:
+                    topo[nodeB] = {}
+                topo[nodeA][nodeB] = idx
+                topo[nodeB][nodeA] = idx
+        idx += 1
+    return topo
+                    
+            
 
 def get_site(nodeid):
     if nodes[nodeid]:
@@ -77,13 +52,11 @@ def get_sitenodes(siteid):
 Find the IP address assigned to a virtual interface in the topology
 (for creating /etc/hosts)
 """
-def get_virt_ip(myid, nodeid):
-    if myid < nodeid:
-        virtip = "10.%d.%d.2" % (myid, nodeid)
-    else:
-        virtip = "10.%d.%d.3" % (nodeid, myid)
-    return virtip
+def get_virt_ip(myid, linkid):
+    return "192.168.%d.%d" % (linkid, myid)
 
+def get_virt_net(linkid):
+    return "192.168.%d.0/24" % linkid
 
 """
 Create a dictionary of site records keyed by site ID
@@ -106,15 +79,18 @@ def get_nodes():
         tmp.append(t)
     return dict(tmp)
 
-adjacencies = gen_adjacencies(links)    
-check_adjacencies(adjacencies)
+
+# For debugging
+dryrun = 1
 
 """ Need global topology information """
 sites = get_sites()
 nodes = get_nodes()
 
+adj_matrix = get_adjacency_matrix(links)
+
 for slice in GetSlices():
-    """ Create dictionary of the slice's attributes """
+    # Create dictionary of the slice's attributes 
     attrs ={}
     topo_attr = {}
     for attribute in GetSliceAttributes(slice['slice_attribute_ids']):
@@ -122,45 +98,59 @@ for slice in GetSlices():
         if attribute['name'] == 'topo_rspec' and attribute['node_id']:
             topo_attr[attribute['node_id']] = attribute['slice_attribute_id']
             
+    if dryrun and slice['name'] == 'pl_trellis':
+        attrs['egre_key'] = 101
+        
     if 'egre_key' in attrs:
         #print "Virtual topology for %s:" % slice['name']
         slicenodes = set(slice['node_ids'])
         hosts = "127.0.0.1\t\tlocalhost\n"
         """
-        For each node in the slice, check whether nodes at adjacent sites
-        are also in the slice's node set.  If so, add a virtual link to 
-        the rspec.  
+        For each node in the slice, check whether there are any adjacent
+        nodes also in the sliceset using the adjacency matrix.
+        For each pair of adjacent nodes, add to nodes' rspecs.
         """
-        for node in slicenodes:
-            topo = []
-            for adj in adjacencies[get_site(node)]:
-                for adj_node in get_sitenodes(adj):
-                    if node != adj_node and adj_node in slicenodes:
-                        link = adj_node, get_ipaddr(adj_node), "1Mbit"
-                        topo.append(link)
-                        shortname = nodes[node]['hostname'].replace('.vini-veritas.net', '')
-                        hosts += "%s\t\t%s\n" % (get_virt_ip(node, adj_node),
-                                                  shortname)
-            topo_str = "%s" % topo
-            #print node, topo_str
-            if node in topo_attr:
+        topo = {}
+        for a in slicenodes:
+            for b in adj_matrix[a]:
+                if b in slicenodes:
+                    if a not in topo:
+                        topo[a] = []
+                    link_id = adj_matrix[a][b]
+                    my_ip = get_virt_ip(a, link_id)
+                    remote_ip = get_virt_ip(b, link_id)
+                    net = get_virt_net(link_id)
+                    link = b, get_ipaddr(b), "1Mbit", my_ip, remote_ip, net
+                    topo[a].append(link)
+                    shortname = nodes[a]['hostname'].replace('.vini-veritas.net', '')
+                    hosts += "%s\t\t%s\n" % (my_ip, shortname)
+
+        for node in topo:
+            topo_str = "%s" % topo[node]
+            if dryrun:
+                print node, topo_str
+            elif node in topo_attr:
                 UpdateSliceAttribute(topo_attr[node], topo_str)
                 del topo_attr[node]
             else:
                 id = slice['slice_id']
                 AddSliceAttribute(id, 'topo_rspec', topo_str, node)
 
-        #print hosts
-        if 'hosts' in attrs:
+        if dryrun:
+            print hosts
+        elif 'hosts' in attrs:
             UpdateSliceAttribute(attrs['hosts'], hosts)
         else:
             id = slice['slice_id']
             AddSliceAttribute(id, 'hosts', hosts)
-    #else:
-        #print "No EGRE key for %s" % slice['name']
-
-    """ Remove old topo_rspec entries """
-    for node in topo_attr:
-        DeleteSliceAttribute(topo_attr[node])
+    else:
+        if dryrun:
+            print "No EGRE key for %s" % slice['name']
+
+    # Remove old topo_rspec entries
+    
+    if not dryrun:
+        for node in topo_attr:
+            DeleteSliceAttribute(topo_attr[node])
 
 
index cef4108..16c366a 100755 (executable)
@@ -9,8 +9,9 @@ REMOTE=$3
 KEY=$4
 RATE=$5
 VIRTIP=$6
+VIRTNET=$7
 
-VIRTNET=`echo $VIRTIP|sed "s/\.[23]$/.0/"`
+SUBNET=`expr match "$VIRTNET" '.*\(/[0-9]*\)'`
 LINK=${KEY}x${NODEID}
 
 modprobe ip_gre
@@ -44,7 +45,7 @@ sleep 1
 PID=`su $SLICE -c "pgrep sleep"`
 chcontext --ctx 1 -- echo $PID > /sys/class/net/$ETUN0/new_ns_pid 
 sleep 1
-su $SLICE -c "sudo /sbin/ifconfig $ETUN0 $VIRTIP/24 up; sudo /sbin/route add -net $VIRTNET/24 dev $ETUN0"
+su $SLICE -c "sudo /sbin/ifconfig $ETUN0 ${VIRTIP}${SUBNET} up; sudo /sbin/route add -net $VIRTNET dev $ETUN0"
 
 ### Set rate
 tc qdisc add dev $EGRE root handle 1: htb default 10
diff --git a/topo.py b/topo.py
index f87a270..e142802 100755 (executable)
--- a/topo.py
+++ b/topo.py
@@ -41,22 +41,14 @@ def virtual_link(key, nodeid):
     else:
         return False
 
-def get_virt_ip(myid, nodeid):
-    if myid < nodeid:
-        virtip = "10.%d.%d.2" % (myid, nodeid)
-    else:
-        virtip = "10.%d.%d.3" % (nodeid, myid)
-    return virtip
-
 """
 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):
+def setup_virtual_link(slice, key, rate, myid, nodeid, ipaddr, virtip, vnet):
     logger.log("%s: Set up virtual link to node %d" % (slice, nodeid))
-    virtip = get_virt_ip(myid, nodeid)
-    run(setup_link_cmd + " %s %s %s %s %s %s" % (slice, nodeid, ipaddr, 
-                                                 key, rate, virtip))
+    run(setup_link_cmd + " %s %s %s %s %s %s %s" % (slice, nodeid, ipaddr, 
+                                                 key, rate, virtip, vnet))
     return
 
 
@@ -151,16 +143,18 @@ Update virtual links for the slice
 """
 def update_links(slice, myid, topospec, key, netns):
     topolist = convert_topospec_to_list(topospec)
-    for (nodeid,ipaddr,rate) in topolist:
+    for (nodeid, ipaddr, rate, myvirtip, remvirtip, virtnet) in topolist:
         if not virtual_link(key, nodeid):
             if netns:
-                setup_virtual_link(slice, key, rate, myid, nodeid, ipaddr)
+                setup_virtual_link(slice, key, rate, myid, nodeid, 
+                                   ipaddr, myvirtip, virtnet)
         else:
             logger.log("%s: virtual link to node %s exists" % (slice, nodeid))
             refresh_virtual_link(nodeid, key)
 
     if not nat_exists(key):
-        setup_nat(slice, myid, key)
+        if netns:
+            setup_nat(slice, myid, key)
     else:
         logger.log("%s: NAT exists" % slice)
         refresh_nat(key)
@@ -195,11 +189,12 @@ Quagga.
 def get_ifaces(hostname, myid, topospec, key):
     ifaces = {}
     topolist = convert_topospec_to_list(topospec)
-    for (nodeid, ipaddr, rate) in topolist:
+    for (nodeid, ipaddr, rate, myvirtip, remvirtip, virtnet) in topolist:
         name = "a%sx%s" % (key, nodeid)
         ifaces[name] = {}
-        ifaces[name]['remote-ip'] = get_virt_ip(nodeid, myid)
-        ifaces[name]['local-ip'] = get_virt_ip(myid, nodeid)
+        ifaces[name]['remote-ip'] = remvirtip
+        ifaces[name]['local-ip'] = myvirtip
+        ifaces[name]['network'] = virtnet
         ifaces[name]['short-name'] = hostname.replace('.vini-veritas.net', '')
     return ifaces
 
@@ -215,31 +210,44 @@ password %s
     return
 
 
+"""
+Network used by OpenVPN on this node
+"""
+def openvpn_net(myid):
+    return "10.%s.0.0/16" % myid
+
+
+"""
+IP address of NAT gateway to outside world
+"""
+def nat_gw(key, myid):
+    return "10.%s.%s.1" % (key, myid)
+
+
 """
 Write zebra.conf file for Quagga
 """
-def write_zebra(filename, myname, ifaces):
+def write_zebra(filename, myname, ifaces, myid, key):
     f = open(filename, 'w')
     password = "zebra"
     write_header(f, myname, password)
 
-    f.write ("""enable password %s
-!
-""" % password)
+    f.write ("enable password %s\n" % password)
 
     for name in ifaces:
         f.write ("""!     
-     interface %s
-     link-detect
+interface %s
+link-detect
 """ %  name)
 
     f.write ("""!
+ip route %s %s
 !
- access-list vty permit 127.0.0.1/32
+access-list vty permit 127.0.0.1/32
 !
- line vty
+line vty
 !
-""")
+""" % (openvpn_net(myid), nat_gw(key, myid)))
     f.close()
     return
 
@@ -270,10 +278,11 @@ def write_ospf(filename, myname, ifaces):
         f.write ("     neighbor %s\n" % ifaces[name]['remote-ip'])
 
     for name in ifaces:
-        net = ifaces[name]['local-ip'].rstrip('23')+'0'
-        f.write ("     network %s/24 area 0\n" % net)
+        net = ifaces[name]['network']
+        f.write ("     network %s area 0\n" % net)
 
-    f.write("""!
+    f.write("""     redistribute static
+!
 access-list vty permit 127.0.0.1/32
 !
 line vty
@@ -296,7 +305,8 @@ def update_quagga_configs(slicename, hostname, myid, topo, key, netns):
             logger.log("topo: could not create %s\n" % quagga_dir)
             return
 
-    write_zebra(quagga_dir + "zebra.conf.generated", hostname, ifaces)
+    write_zebra(quagga_dir + "zebra.conf.generated", hostname, ifaces, 
+                myid, key)
     write_ospf(quagga_dir + "ospfd.conf.generated", hostname, ifaces)
 
     # Start up Quagga if we installed it earlier and netns = 1.