Setup and teardown NAT interfaces automatically
authorAndy Bavier <acb@cs.princeton.edu>
Fri, 20 Feb 2009 21:04:47 +0000 (21:04 +0000)
committerAndy Bavier <acb@cs.princeton.edu>
Fri, 20 Feb 2009 21:04:47 +0000 (21:04 +0000)
NodeManager-topo.spec
topo.py

index 45e5981..f33a10e 100644 (file)
@@ -2,7 +2,7 @@
 
 Name:          NodeManager-topo
 Version:       0.2
-Release:       1
+Release:       2
 Summary:       Plugin supporting creating a default virtual topology.
 
 Group:         System Environment/Daemons
diff --git a/topo.py b/topo.py
index 8174dbf..502b605 100755 (executable)
--- a/topo.py
+++ b/topo.py
@@ -13,9 +13,12 @@ import re
 import vserver
 import os
 
-dryrun=0
-setup_link_cmd="/usr/share/vini/setup-egre-link"
-teardown_link_cmd="/usr/share/vini/teardown-egre-link"
+dryrun = 0
+vinidir = "/usr/share/vini/"
+setup_link_cmd = vinidir + "setup-egre-link"
+teardown_link_cmd = vinidir + "teardown-egre-link"
+setup_nat_cmd = vinidir + "setup-nat"
+teardown_nat_cmd = vinidir + "teardown-nat"
 ifaces = {}
 old_ifaces = {}
 
@@ -67,11 +70,49 @@ def teardown_virtual_link(key, nodeid):
 Called for all active virtual link interfaces, so they won't be cleaned up.
 """
 def refresh_virtual_link(nodeid, key):
-    try:
-        name = "d%sx%s" % (key, nodeid)
+    name = "d%sx%s" % (key, nodeid)
+    if name in old_ifaces:
         del old_ifaces[name]
-    except:
-        pass
+    return
+
+
+"""
+Check for existence of interface natx<key>
+"""
+def nat_exists(key):
+    name = "natx%s" % key
+    if name in ifaces:
+        return True
+    else:
+        return False
+
+
+"""
+Create a NAT interface inside the sliver.  
+"""
+def setup_nat(slice, myid, key):
+    logger.log("%s: Set up NAT" % slice)
+    run(setup_nat_cmd + " %s %s %s" % (slice, myid, key))
+    return
+
+
+"""
+Tear down the NAT interface identified by key
+"""
+def teardown_nat(key):
+    logger.log("topo: Tear down NAT %s" % key)
+    run(teardown_nat_cmd + " %s" % key)
+    return
+
+
+"""
+Called for all active NAT interfaces, so they won't be cleaned up.
+"""
+def refresh_nat(key):
+    name = "natx%s" % (key)
+    if name in old_ifaces:
+        del old_ifaces[name]
+    return
 
 
 """
@@ -79,14 +120,21 @@ Clean up old virtual links (e.g., to nodes that have been deleted
 from the slice).
 """
 def clean_up_old_virtual_links():
-    pattern = "d(.*)x(.*)"
+    pattern1 = "d(.*)x(.*)"
+    pattern2 = "natx(.*)"
     for iface in old_ifaces:
-        m = re.match(pattern, iface)
+        m = re.match(pattern1, iface)
         if m:
             key = int(m.group(1))
             node = int(m.group(2))
             teardown_virtual_link(key, node)
 
+        m = re.match(pattern2, iface)
+        if m:
+            key = int(m.group(1))
+            teardown_nat(key)
+    return
+
 
 """
 Not the safest thing to do, probably should use pickle() or something.
@@ -108,6 +156,13 @@ def update(slice, myid, topospec, key, netns):
             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)
+    else:
+        logger.log("%s: NAT exists" % slice)
+        refresh_nat(key)
+
+
 """
 Write /etc/vservers/<slicename>/spaces/net
 """